[racket] performance tuning summing a hash of vectors

From: Marijn (hkBst at gentoo.org)
Date: Wed Mar 14 03:26:43 EDT 2012

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 13-03-12 17:34, Matthias Felleisen wrote:
> 
> On Mar 13, 2012, at 12:05 PM, Marijn wrote:
> 
>> (define (sum-hash-of-vectors3 table keys size) (apply vector-map
>> + (for/list ((k keys)) (hash-ref table k))))
> 
> Aren't the last two lines just
> 
> (hash-map table (lambda (key table) table))

Indeed they are mostly the same and using that shaves off another ~5%
of the runtime.

(define (sum-hash-of-vectors3.1 table keys size)
  (apply vector-map +
	 (hash-map table (lambda (key val) val))))

There is a slight difference in that the traversal is now implicitly
over all keys while with the above it is possible to specify a subset
of keys. I used to rely on the possibility to leave non-current items
in the table to remember user input in case they wanted to restore the
data again, although I have to check whether or not that is still the
case.

Anyway, thanks for mentioning that.

> ;; -------------------------------------------
> 
> When it comes to performance, always try to trust the compiler
> first. It was eons ago that we had much intuition of how to turn
> code into machine instructions, and how much these instructions
> would cost. This is even true for C now (think pipelines, memory
> hierarchies, etc).

I try to do that and I try to listen to my betters, but I guess
nothing beats being surprised by the compiler. ;P

Marijn

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9gSDMACgkQp/VmCx0OL2zwtgCdG2bcOp5DOLwXJSEdao+lu19+
UG0An04aE8fNMQDpHMguThrSWsPDL6+q
=sliE
-----END PGP SIGNATURE-----

Posted on the users mailing list.