-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_split.cpp
145 lines (127 loc) · 4.68 KB
/
test_split.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Created by yche on 5/3/18.
//
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include "../fast_tokenizer/tokenizer.h"
//const char *my_str = "interface=com.alibaba.dubbo.performance.demo.provider.IHelloService&"
// "method=hash¶meterTypesString=Ljava%2Flang%2FString%3B"
// "¶meter=abc";
const char *my_str = "interface=com.alibaba.dubbo.performance.demo.provider.IHelloService&"
"method=hash¶meterTypesString=Ljava%2Flang%2FString%3B"
"¶meter=Tbk4ZGqnHQNRM8Wqr65Sxz8K2wnWHhvcaNuAnTn64geI6AnEHB8cCtEg154"
"rqjTqWIXqMdUwysbjwTivtkbLi8qNHWg1Kri58NPcKS3mpe1lZ0bh48dDhgoNRoAkL548Lz"
"GKQNiZb2cKX6geL7srwMjbZ1ybLvCNkHFnMd6cpIMMO8mvyAA3LLH3RydyTGEv2JLP6N9lp"
"3xE1YnI8O28TMu0XKeqBaNzpHQDrNDAMSNZn7Ka896JnEiFr1naM5h7fyff1BqXUfA5rZSed"
"kSXQt4RyY9WvmhEeF3eOfapdb34P64uLlJqfNcBpjfKB5sHaeUmDpBI6j18XhxqvdeE2K69dFS"
"dXavGri0aLyeUlCUX3VE5owNt27yeYNH76qHEC0ZJpBW9BnUTo4BuojqhnyfIkdTb7IEl8lyns"
"6uJPJXTSIuZloVqCcZxoqvh9dlj69BIVLaksjhttv2lAuF9raq5ceXkHWrsgfLEvwphvCc5gHAgliG";
long timediff(clock_t t1, clock_t t2) {
long elapsed;
elapsed = (long) (((double) t2 - t1) / CLOCKS_PER_SEC * 1000);
return elapsed;
}
void test_split() {
auto *buffer = static_cast<char *>(malloc(sizeof(char) * 4096));
auto *off = static_cast<short *>(malloc(sizeof(short) * 16));
auto len = (short) strlen(my_str);
memcpy(buffer, my_str, len);
// 1st: usage demo
int size = simd_split_efficient(buffer, len, off);
printf("%s, \n", my_str);
printf("=,& : %d, %d\n", '=', '&');
printf("%d, %d\n", len, size);
for (int i = 0; i < size; i++) { printf("%d\n", off[i]); }
// 2nd: measure time -----------------
// 1) avx - efficient
clock_t t1, t2;
long elapsed;
t1 = clock();
for (int i = 0; i < 5000 * 60; i++) {
simd_split_efficient(buffer, len, off);
}
t2 = clock();
elapsed = timediff(t1, t2);
printf("simd elapsed: %ld ms\n", elapsed);
// 2) serial
t1 = clock();
for (int i = 0; i < 5000 * 60; i++) {
serial_split(buffer, len, off);
}
t2 = clock();
elapsed = timediff(t1, t2);
printf("serial elapsed: %ld ms\n", elapsed);
// end of measuring time --------------------------
free(off);
free(buffer);
}
void test_generate_body() {
printf("-------------------\n");
auto len = (short) strlen(my_str);
auto *str_buffer = static_cast<char *>(malloc(sizeof(char) * 4096));
memcpy(str_buffer, my_str, len);
// 1st: hard code
clock_t t1, t2;
long elapsed;
int64_t req_id = 0;
t1 = clock();
for (int i = 0; i < 5000 * 60; i++) {
auto *res = static_cast<char *>(malloc(sizeof(char) * 2048));
generate_res_in_place_hard_code(res, str_buffer, len, req_id);
req_id++;
free(res);
}
t2 = clock();
elapsed = timediff(t1, t2);
printf("total hard code transform time: %ld ms\n\n", elapsed);
// 2nd: serial
t1 = clock();
for (int i = 0; i < 5000 * 60; i++) {
auto *res = static_cast<char *>(malloc(sizeof(char) * 2048));
generate_res_in_place_serial(res, str_buffer, len, req_id);
req_id++;
free(res);
}
t2 = clock();
elapsed = timediff(t1, t2);
printf("total serial transform time: %ld ms\n\n", elapsed);
// 3rd: sse
t1 = clock();
for (int i = 0; i < 5000 * 60; i++) {
auto *res = static_cast<char *>(malloc(sizeof(char) * 2048));
generate_res_in_place_sse(res, str_buffer, len, req_id);
req_id++;
free(res);
}
t2 = clock();
elapsed = timediff(t1, t2);
printf("total sse4 transform time: %ld ms\n\n", elapsed);
// 4th: adaptive optimal avx2
t1 = clock();
for (int i = 0; i < 5000 * 60; i++) {
auto *res = static_cast<char *>(malloc(sizeof(char) * 2048));
generate_res_in_place(res, str_buffer, len, req_id);
req_id++;
free(res);
}
t2 = clock();
elapsed = timediff(t1, t2);
printf("total avx2 transform time: %ld ms\n\n", elapsed);
// last round print out the results
printf("-------------------\n");
auto *res = (char *) malloc(sizeof(char) * 2048);
int offset = generate_res_in_place(res, str_buffer, len, req_id);
printf("header:");
for (int i = 0; i < 16; i++) { printf("0x%x ,", res[i] & 0xff); }
printf("\ntotal length: %d\n", offset - 16);
printf("%.*s", offset - 16, res + 16);
printf("after the new line~~~~~~~~~");
free(str_buffer);
free(res);
}
int main() {
// test_split();
test_generate_body();
}