我有一个程序,从一组1000000(可能包含重复项)生成10000个整数。 文件生成器功能如下:
void generateNumberFile() {
const char* filename = "/tmp/integerfile.csv";
FILE *numberFile = fopen(filename, "w");
int number_idx;
int random_integer;
struct timeval t1;
for (number_idx = 0; number_idx < NUMBER_OF_ELEMENTS - 1; number_idx++) {
gettimeofday(&t1, NULL);
srand(t1.tv_usec * t1.tv_sec);
random_integer = ((float)rand()/(float)RAND_MAX) * NUMBER_OF_INTEGERS;
if (number_idx == 0)
printf("random number at idx %d : %d\n", number_idx, random_integer);
fprintf(numberFile, "%d,", random_integer);
}
random_integer = ((float)rand()/(float)RAND_MAX) * NUMBER_OF_INTEGERS;
fprintf(numberFile, "%d", random_integer);
printf("last number generated : %d\n", random_integer);
fclose(numberFile);
}
然后我在读取带有数字的文件(下面)时对位数组进行了排序:
double space_optimized_sort(int *bit_array) {
clock_t begin, end;
double time_spent;
begin = clock();
char *filename = "/tmp/integerfile.csv";
FILE *numberFile = fopen(filename, "r");
char* number = malloc(10*sizeof(char));
char* digit = malloc(2*sizeof(char));
int int_number;
while ((digit = fgets(digit, 2, numberFile)) != NULL) {
digit[1] = '\0';
if (strcmp(digit, ",") == 0) {
sscanf(number, "%d", &int_number);
bit_array[int_number >> sizeof(int)] &= (1 << (int_number & MASK));
*number = '\0';
} else {
strcat(number, digit);
}
}
fclose(numberFile);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
return time_spent;
}
然后我实现了另一个函数,它将所有数字读入内存,然后使用qsort对它们进行排序。
int integer_cmp(const void *keyval, const void *datum) {
int i1 = *(int*)keyval;
int i2 = *(int*)datum;
return i1 - i2;
}
double sort_system_function() {
clock_t begin, end;
double time_spent;
begin = clock();
char *filename = "/tmp/integerfile.csv";
FILE *numberFile = fopen(filename, "r");
char* number = malloc(10*sizeof(char));
char* digit = malloc(2*sizeof(char));
int int_number;
int number_array[NUMBER_OF_ELEMENTS];
int element_counter = 0;
while ((digit = fgets(digit, 2, numberFile)) != NULL) {
digit[1] = '\0';
if (strcmp(digit, ",") == 0) {
sscanf(number, "%d", &int_number);
number_array[element_counter] = int_number;
element_counter++;
*number = '\0';
} else {
strcat(number, digit);
}
}
fclose(numberFile);
// sort using the std lib
qsort(number_array, NUMBER_OF_ELEMENTS, sizeof(int), &integer_cmp);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
return time_spent;
}
主要功能:
int main(int argc, char** argv) {
int number_of_bytes = NUMBER_OF_INTEGERS / INT_SIZE;
int iteration_counter;
double total_time_spent = 0;
double average_time_spent = 0;
for (iteration_counter = 0; iteration_counter < NUMBER_ITERATIONS; iteration_counter++) {
generateNumberFile();
total_time_spent += sort_system_function();
}
average_time_spent = total_time_spent / NUMBER_ITERATIONS;
printf("system sort average time spent : %f\n", average_time_spent);
int *bit_array = malloc(number_of_bytes*sizeof(int));
total_time_spent = 0.0;
for (iteration_counter = 0; iteration_counter < NUMBER_ITERATIONS; iteration_counter++) {
generateNumberFile();
total_time_spent += space_optimized_sort(bit_array);
}
average_time_spent = total_time_spent / NUMBER_ITERATIONS;
printf("space constrained sort average time spent : %f\n", average_time_spent);
return 0;
}
在没有分析的情况下运行程序会给出以下输出(在mac上):
system sort average time spent : 0.016194
space constrained sort average time spent : 0.014640
由于我的mac上没有gprof,我在VMWare Fusion上运行的Ubuntu VM中运行了相同的程序。 我获得的值与我在本机操作系统上获得的值不同(运行时没有编译成可执行文件的分析信息):
system sort average time spent : 0.007830
space constrained sort average time spent : 0.006710
然后我使用-pg(profiling)标志编译代码并在VM上运行它。然后在输出上运行gprof。 结果如下:
system sort average time spent : 0.008800
space constrained sort average time spent : 0.005600
gprof的输出位于下图中(无法复制粘贴):
两个问题:
为什么本机操作系统运行和VM运行(后者更小)的数量级不同?
在gprof输出(第一张图片)中,函数'integer_cmp'所花费的时间是否包含在函数'sort_system_function'中? (这是使用它的sort函数调用的地方)