3 ms·
These things invariably report misleading results just because the author can't be bothered to learn to write idiomatic code in most of the languages. Nobody i
by ncmncm 6y ago
These things invariably report misleading results just because the author can't be bothered to learn to write idiomatic code in most of the languages.
Nobody is obliged to learn that many languages well, but truth demands scruples. If you are not interested in truth, what is the point of publishing one of these?
The result is predictable: whichever language the author already knows best wins.
In this one, for example, we get:
"There’s obviously a lot more pushing you could do with C++. However, I suspect it would end up getting more and more low-level and more C-like"
which is just wholly false.
There are reasons why the people who are the most serious about performance use C++: you can get C++ code as fast as the machine can physically do without giving up any abstraction or readability, and without avoiding powerful features. That is the core value proposition of the language: abstraction without penalty. It delivers, and not by accident.
To get top performance does require awareness of what operations are inherently slow for machines, and not writing code to require such operations. But that doesn't mean giving anything up.
Code that looks like C is no faster than C. Hence, to make code faster than C, you must make it look less like C.
Really, we don't expect an article like this to reveal all about how to write the best code in all languages.
Just don't report lazy falsehoods.
- burntsushi 6y agoWhy not write the C++ program? It's not like the OP presented the C++ program as the shining example of what the best looks like. They inserted plenty of caveats. And FWIW, I ported the optimized C program to Rust. They look very similar even though Rust is all about zero cost abstractions as well. So the OP's claim really isn't that ridiculous.
- ncmncm 6y agoIn the old days we said, "You can write Fortran in any language", meaning you can write bad code in any language. But that is not a reason to write bad code. Idiomatic Rust, like idiomatic C++, is faster than C. So, your C-in-Rust runs like C, slow, not like good Rust or good C++, fast.
- burntsushi 6y agoI'd be happy to be shown that I'm wrong.
- ncmncm 6y agoOK, hold my beer. #include <algorithm> #include <cctype> #include <iostream> #include <iterator> #include <string> #include <unordered_map> #include <vector> int main() { std::unordered_map<std::string, int> counts; std::string word; word.reserve(1024); for (std::istreambuf_iterator<char> in(std::cin), end; in != end; ++in) { unsigned char c = *in; if (std::isspace(c)) { if (!word.empty()) { ++counts[word]; word.clear(); } } else word.push_back(std::tolower(c)); } if (!word.empty()) { // in case no EOL ++counts[word]; } std::vector<std::pair<const std::string,int> const*> vec; vec.reserve(1<<15); for (auto const& word : counts) { vec.push_back(&word); } std::sort(vec.begin(), vec.end(), [](auto const* a, auto const* b) { return a->second > b->second; }); for (auto const* word : vec) { std::cout << word->first << ' ' << word->second << '\n'; } }
- burntsushi 6y agoDid you even bother to compare it to other programs? It's dog slow. $ hyperfine './optimized-ncmncm-cpp < kjvbible_x10.txt' Benchmark #1: ./optimized-ncmncm-cpp < kjvbible_x10.txt Time (mean ± σ): 1.396 s ± 0.023 s [User: 1.381 s, System: 0.012 s] Range (min … max): 1.357 s … 1.429 s 10 runs $ hyperfine './optimized-cpp < kjvbible_x10.txt' Benchmark #1: ./optimized-cpp < kjvbible_x10.txt Time (mean ± σ): 407.6 ms ± 9.1 ms [User: 400.3 ms, System: 6.3 ms] Range (min … max): 396.6 ms … 423.6 ms 10 runs $ hyperfine './optimized-c < kjvbible_x10.txt' Benchmark #1: ./optimized-c < kjvbible_x10.txt Time (mean ± σ): 203.6 ms ± 4.7 ms [User: 195.1 ms, System: 8.0 ms] Range (min … max): 196.2 ms … 211.1 ms 14 runs $ hyperfine './rust/optimized/target/release/countwords < kjvbible_x10.txt' Benchmark #1: ./rust/optimized/target/release/countwords < kjvbible_x10.txt Time (mean ± σ): 314.6 ms ± 6.9 ms [User: 302.0 ms, System: 12.1 ms] Range (min … max): 307.6 ms … 328.9 ms 10 runs $ hyperfine './rust/optimized-customhashmap/target/release/countwords < kjvbible_x10.txt' Benchmark #1: ./rust/optimized-customhashmap/target/release/countwords < kjvbible_x10.txt Time (mean ± σ): 239.0 ms ± 2.8 ms [User: 231.0 ms, System: 7.5 ms] Range (min … max): 236.0 ms … 245.0 ms 12 runs I compiled with g++ -O3 optimized-ncmncm.cpp -o optimized-ncmncm-cpp I also tried with clang++ and got similarish results. Care to retract any of your statements? Did you even bother to look at the programs before spouting off a bunch of nonsense here?