computestatsf.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <fcntl.h>
  2. #include <sys/ioctl.h>
  3. #include <stdio.h>
  4. #include <sys/time.h>
  5. #include <sys/types.h>
  6. #include <sys/mman.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. char *pname;
  11. char *in_file;
  12. #define DATA_COUNT (1024*1024)
  13. double data_items[DATA_COUNT];
  14. int num_data_items = 0;
  15. #define BUFSIZE 1024
  16. char in_buf[BUFSIZE];
  17. static int
  18. compare_double(const void *p1, const void *p2)
  19. {
  20. double val1 = *(u_int64_t *)p1;
  21. double val2 = *(u_int64_t *)p2;
  22. if (val1 == val2)
  23. return 0;
  24. if (val1 < val2)
  25. return -1;
  26. return 1;
  27. }
  28. int
  29. main(int argc, char **argv)
  30. {
  31. FILE *in_fp;
  32. double sum_x = 0;
  33. double sum_sq_x = 0;
  34. double mean;
  35. double std_dev;
  36. int i;
  37. int one_sd = 0;
  38. int two_sd = 0;
  39. int three_sd = 0;
  40. double one_sd_low, one_sd_high;
  41. double two_sd_low, two_sd_high;
  42. double three_sd_low, three_sd_high;
  43. pname = argv[0];
  44. if (argc == 1)
  45. in_fp = stdin;
  46. else {
  47. in_file = argv[1];
  48. in_fp = fopen(in_file, "r");
  49. }
  50. while (fgets(in_buf, BUFSIZE, in_fp)) {
  51. if (num_data_items == DATA_COUNT) {
  52. fprintf(stderr,
  53. "DATA overflow, increase size of data_items array\n");
  54. exit(1);
  55. }
  56. sscanf(in_buf, "%lf", &data_items[num_data_items]);
  57. #if 0
  58. printf("%lf\n", data_items[num_data_items]);
  59. #endif
  60. num_data_items++;
  61. }
  62. if (num_data_items == 0) {
  63. fprintf(stderr, "Empty input file ?\n");
  64. exit(1);
  65. }
  66. #if 0
  67. printf("Total items %lu\n", num_data_items);
  68. #endif
  69. for (i = 0 ; i < num_data_items ; i++) {
  70. sum_x += data_items[i];
  71. sum_sq_x += data_items[i] * data_items[i];
  72. }
  73. mean = sum_x / num_data_items;
  74. printf("\tMean %.4f\n", mean);
  75. std_dev = sqrt((sum_sq_x / num_data_items) - (mean * mean));
  76. printf("\tStd Dev %.4f (%.4f%% of mean)\n",
  77. std_dev, (std_dev * 100.0) / mean);
  78. one_sd_low = mean - std_dev;
  79. one_sd_high = mean + std_dev;
  80. two_sd_low = mean - (2 * std_dev);
  81. two_sd_high = mean + (2 * std_dev);
  82. three_sd_low = mean - (3 * std_dev);
  83. three_sd_high = mean + (3 * std_dev);
  84. for (i = 0 ; i < num_data_items ; i++) {
  85. if (data_items[i] >= one_sd_low &&
  86. data_items[i] <= one_sd_high)
  87. one_sd++;
  88. if (data_items[i] >= two_sd_low &&
  89. data_items[i] <= two_sd_high)
  90. two_sd++;
  91. if (data_items[i] >= three_sd_low &&
  92. data_items[i] <= three_sd_high)
  93. three_sd++;
  94. }
  95. printf("\tWithin 1 SD %.2f%%\n",
  96. ((double)one_sd * 100) / num_data_items);
  97. printf("\tWithin 2 SD %.2f%%\n",
  98. ((double)two_sd * 100) / num_data_items);
  99. printf("\tWithin 3 SD %.2f%%\n",
  100. ((double)three_sd* 100) / num_data_items);
  101. printf("\tOutside 3 SD %.2f%%\n",
  102. ((double)(num_data_items - three_sd) * 100) / num_data_items);
  103. /* Sort the data to get percentiles */
  104. qsort(data_items, num_data_items, sizeof(u_int64_t), compare_double);
  105. printf("\t50th percentile %lf\n", data_items[num_data_items / 2]);
  106. printf("\t75th percentile %lf\n", data_items[(3 * num_data_items) / 4]);
  107. printf("\t90th percentile %lf\n", data_items[(9 * num_data_items) / 10]);
  108. printf("\t99th percentile %lf\n", data_items[(99 * num_data_items) / 100]);
  109. }