#include #include #include #ifdef __GNUC__ #include #endif #include void startSleep(int second) { while (second) { printf("%d\n", second); #ifdef __GNUC__ sleep(1); #endif --second; } } void calcMalloc(int count, int size) { double countBackup = count; clock_t t1 = clock(); count = count * 1000; while (count--) { char *buf = (char *)malloc(size); free(buf); } clock_t t2 = clock(); printf("CLOCKS_PER_SECOND is %d.\n", CLOCKS_PER_SEC); printf("cost is %d, average is %f.\n", t2 - t1, (t2 - t1)/countBackup/CLOCKS_PER_SEC); } void calcMemset(int count, int size) { double countBackup = count; char *buf = (char *)malloc(size); clock_t t1 = clock(); while (count--) { memset(buf, 0, size); memset(buf, 1, size); } clock_t t2 = clock(); printf("CLOCKS_PER_SECOND is %d.\n", CLOCKS_PER_SEC); printf("cost is %d, average is %f.\n", t2 - t1, (t2 - t1)/countBackup/CLOCKS_PER_SEC); } void doMemset(int size) { while (1) { char *buf = (char *)malloc(size); memset(buf, 0, size); memset(buf, 1, size); free(buf); } } int main(int argc, char **argv) { int second = atoi(argv[1]); int count = atoi(argv[2]); int size = atoi(argv[3]); printf("second is %d, count is %d, size is %d.\n", second, count, size); startSleep(second); calcMalloc(count, size); calcMemset(count, size); doMemset(size); return EXIT_SUCCESS; }