-
Notifications
You must be signed in to change notification settings - Fork 42
/
bench.cc
207 lines (183 loc) · 6.69 KB
/
bench.cc
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// timing - rlyeh, public domain [ref] https://gist.github.com/r-lyeh/07cc318dbeee9b616d5e {
#pragma once
#include <thread>
#include <chrono>
#if !defined(TIMING_USE_OMP) && ( defined(USE_OMP) || defined(_MSC_VER) /*|| defined(__ANDROID_API__)*/ )
# define TIMING_USE_OMP
# include <omp.h>
#endif
struct timing {
static double now() {
# ifdef TIMING_USE_OMP
static auto const epoch = omp_get_wtime();
return omp_get_wtime() - epoch;
# else
static auto const epoch = std::chrono::steady_clock::now(); // milli ms > micro us > nano ns
return std::chrono::duration_cast< std::chrono::microseconds >( std::chrono::steady_clock::now() - epoch ).count() / 1000000.0;
# endif
}
template<typename FN>
static double bench( const FN &fn ) {
auto took = -now();
return ( fn(), took + now() );
}
static void sleep( double secs ) {
std::chrono::microseconds duration( (int)(secs * 1000000) );
std::this_thread::sleep_for( duration );
}
};
// } timing
#include <algorithm>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#include <stdexcept>
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
std::string base( const std::string &app = std::string() ) {
TCHAR dest[MAX_PATH];
size_t destSize = MAX_PATH;
if (!dest) return "./" + app;
if (MAX_PATH > destSize) return "./" + app;
DWORD length = GetModuleFileName( NULL, dest, destSize );
PathRemoveFileSpec(dest);
for( auto &ch : dest ) {
if( ch == '\\' ) ch = '/';
}
return std::string() + dest + "/" + app;
}
int main( int argc, const char **argv ) try {
int ret = 0;
// benchmark
if( argc > 1 )
{
int N = 0;
std::string cmd, sep, desc;
for( int i = 1; i < argc; ++i ) {
if( argv[i][0] >= '0' && argv[i][0] <= '9' ) {
N = std::strtoul( argv[i], NULL, NULL );
}
else
if( argv[i][0] == '/' && argv[i][1] == '/' ) {
desc = &argv[i][2];
}
else {
cmd += sep + argv[i];
sep = ' ';
if( desc.empty() ) desc = argv[i];
}
}
auto overhead = 0; // measure fib(0)
auto took = timing::bench( [&]{
// avg time
for( auto i = 0; i < N - 1; ++i )
system((cmd+" 1> nul 2> nul").c_str());
ret = system(cmd.c_str());
} );
std::cout << (std::abs(took - overhead) / N) << " s." << std::endl;
// append to 'bench.csv'
{
std::ofstream ofs( base("/bench.csv").c_str(), std::ios::binary | std::ios::app );
if( ofs.good() ) {
ofs << desc << "," << ( std::abs(took - overhead) / N ) << "\r\n"; //std::endl;
}
}
}
// update 'bench.md'
{
auto tokenize = []( const std::string &self, const std::string &delimiters ) -> std::vector< std::string > {
std::string map( 256, '\0' );
for( auto &ch : delimiters )
map[ ch ] = '\1';
std::vector< std::string > tokens(1);
for( const unsigned char &ch : self ) {
/**/ if( !map.at(ch) ) tokens.back().push_back( ch );
else if( tokens.back().size() ) tokens.push_back( std::string() );
}
while( tokens.size() && !tokens.back().size() ) tokens.pop_back();
return tokens;
};
struct idx {
std::string name;
float time;
float relative_speed( float min, float max ) const {
float tstep = time / min;
return 100 / (tstep);
}
bool operator <( const idx &other ) const {
if( time < other.time ) return true;
return name < other.name;
}
bool operator==( const idx &other ) const {
return name == other.name;
}
bool operator!=( const idx &other ) const {
return name != other.name;
}
};
std::map< std::string, float > unique;
std::set< idx > sort;
{
std::ifstream ifs( base("/bench.csv").c_str(), std::ios::binary);
std::stringstream ss;
ss << ifs.rdbuf();
auto lines = tokenize( ss.str(), "\r\n" );
for( auto &line : lines ) {
for( auto &ch : line ) {
if( ch == '\\' ) ch = '/';
}
auto tokens = tokenize( line, "," );
if( tokens.size() == 3 ) {
if( tokens[1] == "fib" || tokens[1] == "fib.exe" ) tokens[1] = "c/vc";
float time = std::strtof( tokens[2].c_str(), NULL );
tokens[1] = tokens[0] + "|" + tokens[1];
float prev = (unique[ tokens[1] ] = unique[ tokens[1] ]);
unique[ tokens[1] ] = prev ? (std::min)( prev, time ) : time;
}
}
for( auto &it : unique ) {
sort.insert( idx{ it.first, it.second } );
}
}
auto min = float(sort.begin()->time);
auto max = float(sort.rbegin()->time);
#if 1
auto cmin = sort.begin(); //find("lua")->time; //float(sort.begin()->time);
while( cmin != sort.end() && cmin->name.find("[lua]") == std::string::npos ) cmin++;
min = float(cmin->time);
#endif
std::ofstream ofs( base("BENCH.md").c_str(), std::ios::binary);
ofs << "|Rank|Language|Flavor|Time|Relative Lua speed|Score|" << std::endl;
ofs << "|---:|:-------|:-----|---:|:----------------:|----:|" << std::endl;
auto rank = 0;
for( auto &it : sort ) {
float speed = it.relative_speed(min, max);
int score( speed );
int factor( speed / 100 );
speed = speed > 100 ? 100 : speed;
char time_str[16]; sprintf(time_str, "%6.3f", it.time);
char factor_str[16]; sprintf(factor_str, "%02d.%01d", score/100, (score%100) / 10 );
ofs << '|' << (++rank) << '|' << it.name << "|" << time_str << " s.|![" << speed << "%](http://progressed.io/bar/" << int(speed);
if( score > 100 ) {
ofs << "?title=x" << factor_str << ")|" << score << " pt|" << std::endl;
} else {
ofs << ")|" << score << " pt|" << std::endl;
}
}
}
return ret;
}
catch( std::exception &e ) {
puts( "error: exception caught" );
puts( e.what() );
return -1;
}
catch(...) {
puts( "error: exception caught" );
return -1;
}