forked from parallel101/course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
47 lines (38 loc) · 993 Bytes
/
main.cpp
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
#include "ticktock.h"
#include "randint.h"
#include <vector>
#include <algorithm>
__attribute__((noinline)) void uppercase_slow(char *p, int n) {
for (int i = 0; i < n; i++) {
if ('a' <= p[i] && p[i] <= 'z')
p[i] = p[i] + 'A' - 'a';
}
//random: 0.020724s
//sorted: 0.004538s
}
__attribute__((noinline)) void uppercase_fast(char *p, int n) {
for (int i = 0; i < n; i++) {
p[i] = ('a' <= p[i] && p[i] <= 'z') ? (p[i] + 'A' - 'a') : p[i];
}
//random: 0.000735s (28x faster)
//sorted: 0.000774s
}
#define uppercase uppercase_fast
int main() {
int n = (int)1e7;
std::vector<char> a(n);
for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}
TICK(random);
uppercase(a.data(), n);
TOCK(random);
for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}
std::sort(a.begin(), a.end());
TICK(sorted);
uppercase(a.data(), n);
TOCK(sorted);
return 0;
}