dmitryikh / rust-vs-cpp-bench Goto Github PK
View Code? Open in Web Editor NEWPerformance benchmark of Rust vs. C++ on simple algorithmic problems
Performance benchmark of Rust vs. C++ on simple algorithmic problems
Hi guy, thanks for this wonderful project. I noticed that, by default, compare_performance.sh runs bench programs N=10 times, but reading input takes much time. And, it's significant that read_vec_w_num in C++ side is very unacceptable. Well. on one side, it's different from Rust side implementation, which use read_line and then parse. As we all know, std::cin with its >> operator is super slow monster. I make a getline-strtoll version, which outperforms Rust version!
std::vector<unsigned int> read_vec_w_num() {
#if 0
size_t n;
std::cin >> n;
std::vector<unsigned int> vec(n, 0);
for (auto& val: vec)
std::cin >> val;
return vec;
#else
char* line = nullptr;
char* lineend = nullptr;
size_t len = 0;
ssize_t k = ::getline(&line, &len, stdin);
size_t n = strtoll(line, &lineend, 10);
size_t save_n = n;
std::vector<unsigned int> vec;
vec.reserve(n);
line = nullptr;
lineend = nullptr;
len = 0;
k = ::getline(&line, &len, stdin);
for (;;) {
n = strtoll(line, &lineend, 10);
if (errno == ERANGE || errno == EINVAL){
std::cerr << "range error or inval, got " << std::endl;
errno = 0;
} else {
if (line != lineend) {
vec.push_back(n);
line = lineend;
} else {
break;
}
}
}
if (vec.size() != save_n) {
std::cerr << save_n << " " << vec.size() << std::endl;
exit(1);
}
return vec;
#endif
}
rust-vs-cpp-bench/mergesort
make bench
Original
../common/compare_performance.sh
Averaging performance of 50 runs..
./main_cpp:
test inp_low: runtime .00336 sec, core .00117 sec
test inp_mid: runtime .26368 sec, core .16441 sec
test inp_hi: runtime 2.86361 sec, core 1.88620 sec
./main_rust:
test inp_low: runtime .00265 sec, core .00137 sec
test inp_mid: runtime .24458 sec, core .18713 sec
test inp_hi: runtime 2.72434 sec, core 2.16611 sec
Using getline-strtoll
../common/compare_performance.sh
Averaging performance of 50 runs..
./main_cpp:
test inp_low: runtime .00305 sec, core .00134 sec
test inp_mid: runtime .23502 sec, core .18775 sec
test inp_hi: runtime 2.64279 sec, core 2.18313 sec
./main_rust:
test inp_low: runtime .00264 sec, core .00136 sec
test inp_mid: runtime .24543 sec, core .18754 sec
test inp_hi: runtime 2.71791 sec, core 2.16083 sec
Benches should be rewriten in the following way:
Right now there are no tests(2) so you can put any garbage inside the code and bench it according to GIGO.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.