A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

Hot answers tagged

22

Without sacrifying something, you can probably gain the most (wall time) by using a hint such as posix_fadvise(POSIX_FADV_WILLNEED). Or, if portability is not paramount, something like readahead (Windows calls that function PrefetchVirtualMemory). Be sure to read the docs and watch for words like "blocking". The reason for wanting to prefetch is that while ...


11

You say your file contains integers only. Yet your parsing code calls trim_lower, which doesn't make sense at all. At least I hope you implemented tolower other than in the C++ standard library, since the latter must not be called with signed char or char as argument. The proc_words function creates lots of std::string objects internally, which is ...


8

Update I made a bare-bones yahtzee solver with no error checking in pure C++ (no mmap). The code is considerably more complex than mmapping, but is more portable, more generic, and seems to work just fine. With a single-producer-single-consumer pattern and 64k buffers(arbitrary) and got (0.97s): $ /usr/bin/time -f "%e %U %S %M" ./a ~/yahtzee-upper-big....


7

Since command line arguments are null-terminated, we can avoid strlen() entirely by just running through the string until we hit '\0'; this also gets rid of contin and i (*p > 0x2F && *p < 0x3A) isn't very obvious, I'd just use '0' and '9' to make it easier to understand the intention and to be consistent with the rest of the code You could use ...


4

Build a userland threaded prefetch In addition of Damon excellent answer, I would like emphasize this: trying to add any optimization only to be limited by disk throughput is a waste of time. It's something that's hard to see and even harder to believe. And so this answer. Your desktop machine probably has more than one CPU, and certainly any server your ...


3

I am going to try to summarise and incorporate some findings from the very good and lively discussion in the comments above. I have put together a "best case". "Best" without going "totally silly", ie no custom SIMD ASM or anything. If the file is OS-cached in RAM the mmap can go very very fast. I have measured up to 7GB/s (140ms for 1GB file). Just with a ...


2

What should the output be if I enter 123456789012345678901234567890? That's producing an integer overflow right now.


Only top voted, non community-wiki answers of a minimum length are eligible