#include #include #include #include #include #include #include #define N 1000000 #define MIN_LEN 20 #define MAX_LEN 50 #define MAX_CHAR 'z' bool cmp(char *a, char *b) { return strcmp(a, b) < 0; } int main(void) { srand(time(NULL)); timeval tv; char* s[N]; // Generate some strings for (int i = 0; i < N; i++) { int len = rand() % (MAX_LEN - MIN_LEN + 1) + MIN_LEN; s[i] = (char*) malloc(len + 1); for (int j = 0; j < len; j++) { s[i][j] = rand() % (MAX_CHAR - 'a' + 1) + 'a'; } s[i][len] = '\0'; } gettimeofday (&tv, NULL); long long t1 = 1000LL * tv.tv_sec + tv.tv_usec / 1000; std::sort(s, s + N, cmp); gettimeofday (&tv, NULL); long long t2 = 1000LL * tv.tv_sec + tv.tv_usec / 1000; printf("Timp: %lld ms\n", t2 - t1); // Print them // for (int i = 0; i < N; i++) { // printf("[%s]\n", s[i]); // } }