japanoise / brainfucc

Just another brainfuck interpretor,
GNU General Public License v3.0
1 stars 0 forks source link

mmap (or buffer) files #2

Open ScoreUnder opened 8 years ago

ScoreUnder commented 8 years ago

I poked around with the code and replaced the stdio file calls with mmap.

Tested on this file: https://github.com/rdebath/Brainfuck/blob/master/testing/Golden.b

score@kirisame ~/src/brainfucc % time stdbuf -o0 ./brainfucc-orig ../brainfuck-interpreter/toys/Golden.b
1.618033988749894848204586834365638117stdbuf -o0 ./brainfucc-orig ../brainfuck-interpreter/toys/Golden.b  13.45s user 4.84s system 99% cpu 18.287 total
score@kirisame ~/src/brainfucc % time stdbuf -o0 ./brainfucc ../brainfuck-interpreter/toys/Golden.b 
1.618033988749894848204586834365638117stdbuf -o0 ./brainfucc ../brainfuck-interpreter/toys/Golden.b  0.72s user 0.00s system 99% cpu 0.728 total

That's a 25x speedup :^)

mmap probably doesn't work on Windows systems though, so you'll probably just want to buffer the whole file into memory. You'll likely get an identical speedup.

BTW, notice how long the original interpreter spends in "system" - that's 4.84 seconds spent on file seeking and re-reading alone.

I leave the rest to you!

japanoise commented 8 years ago

Being as we spend a lot of time re-reading the file during loops, this is a very good idea.