Re:Performance comparison: counting words in Python, Go, C++...
by cornstalks on Monday March 15, 2021 @12:47PM
(#26468330)
I dug into this once because reading from stdin on my Mac was so slow, but only when using std::cin. Using C APIs to read stdin was fast. Turns out libc++ on mac uses `getc` to read from stdin one character at a time. Madness.
Re:Performance comparison: counting words in Python, Go, C++...
by darwingr on Tuesday March 16, 2021 @12:57AM
(#26473179)
Huh, well that makes a lot of sense. Three years ago when I was in University practicing for competitive coding we came across a limit in the performance (ms) we could get on even simple problems. The professor leading the practice poked away at his own solution to some problem for days because he couldn't get a better time than the top score online. Eventually he implemented his own versions of cin and cout. Needless to say he was pretty frustrated but there's just some problems that are hard to let go of without a solution.
Re:Performance comparison: counting words in Python, Go, C++...
by w-m on Monday March 15, 2021 @12:50PM
(#26468361)
Similar here, reading the file from stdin on a 2020 MBP on macOS 11.2.2 is incredibly slow.
int main() {
std::ios::sync_with_stdio(false);
std::string str((std::istreambuf_iterator<char>(std::cin)),
std::istreambuf_iterator<char>());
}
takes around 8 seconds to run, no counting at all. int main() {
std::ifstream f("kjvbible_x10.txt");
std::string str((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
}
runs in ~270 miliseconds. Factor 30x, yikes.
Re:Performance comparison: counting words in Python, Go, C++...
by c-cube on Monday March 15, 2021 @09:50AM
(#26466072)
Could be a lock on stdin that is acquired in a loop? Rust has such a lock and typically one should lock stdin at the beginning and read from within the critical section.
Re:Performance comparison: counting words in Python, Go, C++...
by FAANG_dream on Monday March 15, 2021 @09:52AM
(#26466087)
Try this maybe? https://codeforces.com/blog/entry/6251
Re:Performance comparison: counting words in Python, Go, C++...
by macgyverismo on Monday March 15, 2021 @10:16AM
(#26466334)
Asuming you mean 'std::ios::sync_with_stdio(false);', that was already present in the original code.
Re:Performance comparison: counting words in Python, Go, C++...
by usefulcat on Monday March 15, 2021 @11:16AM
(#26466987)
I tried that on Linux and got at best a 5% improvement.
[edit]
more like 9~10x actually...
changes between these two: