Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/benchmark/benchmark_memset.cpp
4133 views
1
// Copyright 2017 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
#include <string.h>
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <vector>
10
#include <iostream>
11
#include <algorithm>
12
13
#ifdef WIN32
14
#include <Windows.h>
15
#define aligned_alloc(align, size) _aligned_malloc((size), (align))
16
#endif
17
18
#ifdef __EMSCRIPTEN__
19
#include <emscripten/emscripten.h>
20
#endif
21
22
#include "tick.h"
23
24
char dst[1024*1024*64+16] = {};
25
26
uint8_t resultCheckSum = 0;
27
28
void __attribute__((noinline)) test_memset(int numTimes, int copySize)
29
{
30
for(int i = 0; i < numTimes - 8; i += 8)
31
{
32
memset(dst, i ^ 0xAA, copySize); resultCheckSum += dst[copySize >> 1];
33
memset(dst, i ^ 0xAA, copySize); resultCheckSum += dst[copySize >> 1];
34
memset(dst, i ^ 0xAA, copySize); resultCheckSum += dst[copySize >> 1];
35
memset(dst, i ^ 0xAA, copySize); resultCheckSum += dst[copySize >> 1];
36
memset(dst, i ^ 0xAA, copySize); resultCheckSum += dst[copySize >> 1];
37
memset(dst, i ^ 0xAA, copySize); resultCheckSum += dst[copySize >> 1];
38
memset(dst, i ^ 0xAA, copySize); resultCheckSum += dst[copySize >> 1];
39
memset(dst, i ^ 0xAA, copySize); resultCheckSum += dst[copySize >> 1];
40
}
41
numTimes &= 15;
42
for(int i = 0; i < numTimes; ++i)
43
{
44
memset(dst, i ^ 0xAA, copySize); resultCheckSum += dst[copySize >> 1];
45
}
46
}
47
48
std::vector<int> copySizes;
49
std::vector<double> results;
50
51
std::vector<int> testCases;
52
53
double totalTimeSecs = 0.0;
54
55
void test_case(int copySize)
56
{
57
const int minimumCopyBytes = 1024*1024*64;
58
59
int numTimes = (minimumCopyBytes + copySize-1) / copySize;
60
if (numTimes < 8) numTimes = 8;
61
62
tick_t bestResult = 1e9;
63
64
#ifndef NUM_TRIALS
65
#define NUM_TRIALS 5
66
#endif
67
68
for(int i = 0; i < NUM_TRIALS; ++i)
69
{
70
double t0 = tick();
71
test_memset(numTimes, copySize);
72
double t1 = tick();
73
if (t1 - t0 < bestResult) bestResult = t1 - t0;
74
totalTimeSecs += (double)(t1 - t0) / ticks_per_sec();
75
}
76
unsigned long long totalBytesTransferred = numTimes * copySize;
77
78
copySizes.push_back(copySize);
79
80
tick_t ticksElapsed = bestResult;
81
if (ticksElapsed > 0)
82
{
83
double seconds = (double)ticksElapsed / ticks_per_sec();
84
double bytesPerSecond = totalBytesTransferred / seconds;
85
double mbytesPerSecond = bytesPerSecond / (1024.0*1024.0);
86
results.push_back(mbytesPerSecond);
87
}
88
else
89
{
90
results.push_back(0.0);
91
}
92
}
93
94
void print_results()
95
{
96
std::cout << "Test cases: " << std::endl;
97
for(size_t i = 0; i < copySizes.size(); ++i)
98
{
99
std::cout << copySizes[i];
100
if (i != copySizes.size()-1) std::cout << ",";
101
else std::cout << std::endl;
102
if (i % 10 == 9) std::cout << std::endl;
103
}
104
std::cout << std::endl;
105
std::cout << std::endl;
106
std::cout << std::endl;
107
std::cout << "Test results: " << std::endl;
108
for(size_t i = 0; i < results.size(); ++i)
109
{
110
std::cout << results[i];
111
if (i != results.size()-1) std::cout << ",";
112
else std::cout << std::endl;
113
if (i % 10 == 9) std::cout << std::endl;
114
}
115
116
std::cout << "Result checksum: " << (int)resultCheckSum << std::endl;
117
std::cout << "Total time: " << totalTimeSecs << std::endl;
118
}
119
120
int numDone = 0;
121
122
void run_one()
123
{
124
std::cout << (numDone+1) << "/" << (numDone+testCases.size()) << std::endl;
125
++numDone;
126
127
int copySize = testCases.front();
128
testCases.erase(testCases.begin());
129
test_case(copySize);
130
}
131
132
#ifdef __EMSCRIPTEN__
133
void main_loop()
134
{
135
if (!testCases.empty())
136
{
137
run_one();
138
}
139
else
140
{
141
emscripten_cancel_main_loop();
142
print_results();
143
}
144
}
145
#endif
146
147
#ifndef MAX_COPY
148
#define MAX_COPY 32*1024*1024
149
#endif
150
151
#ifndef MIN_COPY
152
#define MIN_COPY 1
153
#endif
154
155
int main()
156
{
157
for(int copySizeI = MIN_COPY; copySizeI < MAX_COPY; copySizeI <<= 1)
158
for(int copySizeJ = 1; copySizeJ <= copySizeI; copySizeJ <<= 1)
159
{
160
testCases.push_back(copySizeI | copySizeJ);
161
}
162
163
std::sort(testCases.begin(), testCases.end());
164
#if defined(__EMSCRIPTEN__) && !defined(BUILD_FOR_SHELL)
165
emscripten_set_main_loop(main_loop, 0, 0);
166
#else
167
while(!testCases.empty()) run_one();
168
print_results();
169
#endif
170
}
171
172