top | item 32034241

(no title)

maffydub | 3 years ago

I don't seem to be able to post on his blog (400 Bad Request), so posting here instead...

Interestingly, it looks like Rust (and C via clang) both generate something like the following as their one-at-a-time main loop:

.LBB5_14: addl (%rcx), %eax addq $4, %rcx cmpq %rdx, %rcx jne .LBB5_14

It's one fewer instruction than the Go version (because it has combined the MOV and the ADD), although I don't know whether that would actually result in higher performance.

I say their "one-at-a-time" main loop, because they actually seem to generate lots of code to try to take advantage of AVX or AVX2 to add 4 or 8 values at a time if available and the array is long enough!

It might be cheating, but summing an iterator over numbers is built-in to Rust, so summing an array is just: v.iter().sum()

The fun thing with this is that it's not just generic over number types, it's also generic over any type of iterator, so I can even use it to sum over the values in a HashMap: hashmap.values().sum()

discuss

order

No comments yet.