使用demo
测试用例 bubble-sort.c 如下
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 | #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #define ARRAY_LEN 30000 static struct timeval tm1; static inline void start() { gettimeofday(&tm1, NULL); } static inline void stop() { struct timeval tm2; gettimeofday(&tm2, NULL); unsigned long long t = 1000 * (tm2.tv_sec - tm1.tv_sec) +\ (tm2.tv_usec - tm1.tv_usec) / 1000; printf("%llu ms\n", t); } void bubble_sort (int *a, int n) { int i, t, s = 1; while (s) { s = 0; for (i = 1; i < n; i++) { if (a[i] < a[i - 1]) { t = a[i]; a[i] = a[i - 1]; a[i - 1] = t; s = 1; } } } } void sort_array() { printf("Bubble sorting array of %d elements\n", ARRAY_LEN); int data[ARRAY_LEN], i; for(i=0; i<ARRAY_LEN; ++i) { data[i] = rand(); } bubble_sort(data, ARRAY_LEN); } int main() { start(); sort_array(); stop(); return 0; } |
cfpgo插桩
1 2 3 | mkdir pgo-profile gcc -O3 -fcfgo-profile-generate=./pgo-frofile bubble-srot.c -o bubble-sort.o ./bubble-sort.o |
cspgo插桩
1 2 3 | mkdir cspgo-profile gcc -O3 -fcfgo-profile-use=./pgo-frofile -fcfgo-csprofile-generate=./cspgo-profile bubble-srot.c -o bubble-sort.o ./bubble-sort.o |
编译cspg优化后二进制
1 2 3 | mkdir cspgo-profile gcc -O3 -fcfgo-profile-use=./pgo-frofile -fcfgo-csprofile-use=./cspgo-profile bubble-srot.c -o bubble-sort.o ./bubble-sort.o |
父主题: CSPGO