Re: [racket-users] Word Count program/benchmark performance

2021-03-20 Thread Bogdan Popa
On top of these changes, replacing the hash function with Chez's `equal-hash` saves another 50ms. That gets the runtime down to around 900ms on my machine, including Racket startup time. Ignoring startup time, this version beats the `simple.c` implementation in the original repo by about 50ms

Re: [racket-users] Word Count program/benchmark performance

2021-03-20 Thread Gustavo Massaccesi
With two additional tricks I saved like 100ms. * Saving the output port instead of reading the parameter implicitly each time. * Replacing (write (cdr p)) with (write-fx cdr p)) where (define (write-fx n [o (current-output-port)]) ; TODO: Add negatives :) (if (fx> n 0) (let loop ([n

Re: [racket-users] Word Count program/benchmark performance

2021-03-19 Thread Sam Tobin-Hochstadt
I went from numbers around 1000 ms to 950 ms to 900 ms. There was variance around those numbers, but it was pretty consistent. For more precise answers, there are a few things you can try. One is to measure instructions instead of time (ie, with perf). Another is to run it a bunch of times and

Re: [racket-users] Word Count program/benchmark performance

2021-03-19 Thread Laurent
Sam: How do you accurately measure such small speed-ups? On my machines, if I run the same program twice, I can sometimes see more than 10% time difference. On Fri, Mar 19, 2021 at 4:10 PM Sam Tobin-Hochstadt wrote: > Use `#:authentic`, and `unsafe-vector*-{ref,set!}` saved about 50 more > ms

Re: [racket-users] Word Count program/benchmark performance

2021-03-19 Thread Sam Tobin-Hochstadt
Use `#:authentic`, and `unsafe-vector*-{ref,set!}` saved about 50 more ms on my machine. Then getting rid of `set!` and just re-binding the relevant variables produced another 50 ms speedup. https://gist.github.com/7fc52e7bdc327fb59c8858a42258c26a Sam On Fri, Mar 19, 2021 at 7:21 AM Sam

Re: [racket-users] Word Count program/benchmark performance

2021-03-19 Thread Laurent
(Welcome to Racket v8.0.0.1 [cs]. ) All results are measured on my laptop on the 10x file with `$ time racket `, thus including the Racket VM. * Bogdan's version with #lang racket/base: 1s. * Dominik's version with vectors of length 256 (instead of 26) and splitting on spaces/return/newline only

Re: [racket-users] Word Count program/benchmark performance

2021-03-19 Thread Bogdan Popa
Nice! It's worth pointing out, though, that by limiting yourself to alpha chars, you're processing about 8% less data and the results don't pass the tests. :P $ wc kjvbible_x10.txt 998170 8211330 43325060 $ sed 's/[a-zA-Z ]//g' < kjvbible_x10.txt | wc 998170 739310 3600800

Re: [racket-users] Word Count program/benchmark performance

2021-03-19 Thread Dominik Pantůček
Another attack of [1]. But yeah, why not do some [2]. Trees to the rescue [3]. $ racket --version Welcome to Racket v8.0 [cs]. $ racket countwords-bogdan2.rkt https://xkcd.com/386/ [2] http://phdcomics.com/comics/archive.php?comicid=1735 [3]

Re: [racket-users] Word Count program/benchmark performance

2021-03-19 Thread Sam Tobin-Hochstadt
One minor additional suggestion: if you use #:authentic for the struct, it will generate slightly better code for the accessors. Sam On Fri, Mar 19, 2021, 6:18 AM Bogdan Popa wrote: > I updated the gist with some cleanups and additional improvements that > get the runtime down to a little over

Re: [racket-users] Word Count program/benchmark performance

2021-03-19 Thread Bogdan Popa
I updated the gist with some cleanups and additional improvements that get the runtime down to a little over 1s (vs ~350ms for the optimized C and Rust code) on my maxed-out 2019 MBP and ~600ms on my M1 Mac Mini. Pawel Mosakowski writes: > Hi Bogdan, > > This is a brilliant solution and also

Re: [racket-users] Word Count program/benchmark performance

2021-03-18 Thread Sorawee Porncharoenwase
string-split always uses regex. I wonder if a fast path when the splitter is a regular string will be worth it. On Fri, Mar 19, 2021 at 4:19 AM Pawel Mosakowski wrote: > Hi Bogdan, > > This is a brilliant solution and also completely over my head. It finishes > in ~3.75s on my PC and is faster

Re: [racket-users] Word Count program/benchmark performance

2021-03-18 Thread Pawel Mosakowski
Hi Bogdan, This is a brilliant solution and also completely over my head. It finishes in ~3.75s on my PC and is faster than the Python version which basically delegates all the work to C. I will need to spend some time on understanding it but I am looking forward to learning something new.

Re: [racket-users] Word Count program/benchmark performance

2021-03-18 Thread Pawel Mosakowski
Hi Sam, Thank you for your responses. Your fastest approach runs in quarter of the time (~5 seconds) of my naive implementation which is pretty amazing. I already had a closer look and can see all the improvements like avoiding string-split, using a mutable hash and modifying it in place,

Re: [racket-users] Word Count program/benchmark performance

2021-03-18 Thread Bogdan Popa
I managed to get it about as fast as Python by making it really imperative and rolling my own hash: https://gist.github.com/Bogdanp/fb39d202037cdaadd55dae3d45737571 Sam Tobin-Hochstadt writes: > Here are several variants of the code: > https://gist.github.com/d6fbe3757c462d5b4d1d9393b72f9ab9 >

Re: [racket-users] Word Count program/benchmark performance

2021-03-18 Thread Sam Tobin-Hochstadt
Here are several variants of the code: https://gist.github.com/d6fbe3757c462d5b4d1d9393b72f9ab9 The enabled version is about the fastest I can get without using `unsafe` (which the rules said not to do). It's possible to optimize a tiny bit more by avoiding sorting, but only a few milliseconds --

Re: [racket-users] Word Count program/benchmark performance

2021-03-18 Thread Sam Tobin-Hochstadt
Here's a somewhat-optimized version of the code: #lang racket/base (require racket/string racket/vector racket/port) (define h (make-hash)) (time (for* ([l (in-lines)] [w (in-list (string-split l))] [w* (in-value (string-downcase w))]) (hash-update! h w* add1 0))) (define v

Re: [racket-users] Word Count program/benchmark performance

2021-03-18 Thread Pawel Mosakowski
Hi David, Yes, the 21 seconds includes the interpreter startup time. I have done a simple test to see how long it takes: $ time racket -e '(displayln "Hello, world")' Hello, world real0m0.479s user0m0.449s sys0m0.030s I have also put my code inside a main function and profiled it:

Re: [racket-users] Word Count program/benchmark performance

2021-03-18 Thread David Storrs
Hi Pawel, I'll take a look at the code later, but did that 21 seconds include startup time for the interpreter? On Thu, Mar 18, 2021, 9:24 AM Pawel Mosakowski wrote: > Hello, > > I am a Racket beginner and I have come across this article: > > >

[racket-users] Word Count program/benchmark performance

2021-03-18 Thread Pawel Mosakowski
Hello, I am a Racket beginner and I have come across this article: https://benhoyt.com/writings/count-words/ This is my attempt at solving the challenge: https://pastebin.com/kL16w5Hc However when I have benchmarked it, it takes ~21 seconds to run compared to the Python and Ruby versions