fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #include <stdint.h>
  5. #include <float.h>
  6. #include <sys/time.h>
  7.  
  8. #define ELEMS 9000000
  9. #define TOPSS 1000
  10.  
  11. typedef double val_t;
  12.  
  13. inline static double baka_sec_count(void) {
  14. struct timeval tv;
  15. gettimeofday(&tv, NULL);
  16. return (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0);
  17.  
  18. }
  19.  
  20. inline static uint64_t rand64(void) {
  21. static uint64_t x = 88172645463325252ULL;
  22. x = x ^ (x << 13);
  23. x = x ^ (x >> 7);
  24. return x = x ^ (x << 17);
  25. }
  26.  
  27. inline static val_t* alloc_data(int n_) {
  28. val_t* p = (val_t*)malloc(sizeof(val_t) * n_);
  29.  
  30. for (int i = 0; i < n_; ++i) {
  31. p[i] = rand64();
  32. }
  33.  
  34. return p;
  35. }
  36.  
  37. inline static void aho_select(val_t* arr_, int n_, val_t* top_, int k_) {
  38.  
  39. for (int i = 0; i < k_; ++i) {
  40. top_[i] = -DBL_MAX;
  41. }
  42.  
  43. for (int i = 0; i < n_; ++i) {
  44. for (int j = 0; j < k_; ++j) {
  45. if (top_[j] < arr_[i]) {
  46. for (int l = k_ - 1; l > j; --l) {
  47. top_[l] = top_[l - 1];
  48. }
  49. top_[j] = arr_[i];
  50. break;
  51. }
  52. }
  53. }
  54. }
  55.  
  56. inline static void free_data(double* p_) {
  57. free(p_);
  58. }
  59.  
  60. int main(int argc, char* argv[]) {
  61. val_t top[1000];
  62. val_t* p1 = alloc_data(ELEMS);
  63. double s;
  64. double e;
  65.  
  66. s = baka_sec_count();
  67. aho_select(p1, ELEMS, top, sizeof(top) / sizeof(val_t));
  68. e = baka_sec_count();
  69. fprintf(stderr, "turnaround time ::= %.04lf sec\n", e-s);
  70.  
  71. free_data(p1);
  72. return 0;
  73. }
  74.  
Success #stdin #stdout #stderr 4.9s 71456KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
turnaround time ::= 4.8076 sec