-
Notifications
You must be signed in to change notification settings - Fork 2
/
time_strcmp.c
58 lines (49 loc) · 1.73 KB
/
time_strcmp.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
57
58
#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "tdiff.h"
extern int strcmp_cmps(const char* s1, const char* s2);
extern int strcmp_lods(const char* s1, const char* s2);
int main(int argc, char* argv[]) {
struct timespec clock1, clock2;
unsigned int count;
assert(strcmp_cmps("abc", "abc") == 0);
assert(strcmp_cmps("abc", "ab") > 0);
assert(strcmp_cmps("ab", "abc") < 0);
assert(strcmp_cmps("abd", "abc") > 0);
assert(strcmp_cmps("abc", "abd") < 0);
assert(strcmp_lods("abc", "abc") == 0);
assert(strcmp_lods("abc", "ab") > 0);
assert(strcmp_lods("ab", "abc") < 0);
assert(strcmp_lods("abd", "abc") > 0);
assert(strcmp_lods("abc", "abd") < 0);
puts("strcmp_cmps():");
clock_gettime(CLOCK_MONOTONIC, &clock1);
for (count = 0; count < 10000000; ++count)
strcmp_cmps("1234567890", "1234567890");
clock_gettime(CLOCK_MONOTONIC, &clock2);
printf("%8lli ms\n", tdiff(clock1, clock2)/1000000);
puts("strcmp_lods():");
clock_gettime(CLOCK_MONOTONIC, &clock1);
for (count = 0; count < 10000000; ++count)
strcmp_lods("1234567890", "1234567890");
clock_gettime(CLOCK_MONOTONIC, &clock2);
printf("%8lli ms\n", tdiff(clock1, clock2)/1000000);
if (argc < 3)
return EXIT_SUCCESS;
puts("arg strcmp_cmps():");
clock_gettime(CLOCK_MONOTONIC, &clock1);
for (count = 0; count < 10000000; ++count)
strcmp_cmps(argv[1], argv[2]);
clock_gettime(CLOCK_MONOTONIC, &clock2);
printf("%8lli ms\n", tdiff(clock1, clock2)/1000000);
puts("arg strcmp_lods():");
clock_gettime(CLOCK_MONOTONIC, &clock1);
for (count = 0; count < 10000000; ++count)
strcmp_lods(argv[1], argv[2]);
clock_gettime(CLOCK_MONOTONIC, &clock2);
printf("%8lli ms\n", tdiff(clock1, clock2)/1000000);
return EXIT_SUCCESS;
}