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