3 ms·
(Re)allocating arrays/vectors seems to be pretty much the whole point, otherwise both Swift and C++ could not only preallocate (reserve), but they could use ite
by masklinn 11y ago
(Re)allocating arrays/vectors seems to be pretty much the whole point, otherwise both Swift and C++ could not only preallocate (reserve), but they could use iterators and elide all allocation instead, and then you'd get something along the lines of this:
> rustc -O test.rs -o test_rs
> time ./test_rs
9999010000
0.54 real 0.53 user 0.00 sys
here's TFA's C++ version by comparison:
> time ./test_cpp
9999010000
3.17 real 2.29 user 0.86 sys
- campoy 11y agoI totally agree with you on this. The code for all three languages is clearly not optimal. I wonder how much of this performance difference is C++ being very good at optimizing your code rather than the runtime being more performant. I don't have any numbers, but I feel like that's not a negligible factor.
- masklinn 11y ago> I totally agree with you on this. The code for all three languages is clearly not optimal. That's not really my point. My point's we have no idea what TFA is trying to do or see[0], so you may be able to say "this is stupid and nonsensical" because it is[1], but you can't say "I've got a better version" then provide something with a completely different behaviour. [0] most likely they just threw some shit at the wall, ended up with relatively long runtimes and called that a benchmark [1] or maybe not, maybe it's a reduction of actual workload in the system doing computation based on some sort of dynamic number of items so you can't preallocate the intermediate storage and the point's to compare reallocation overhead[2] [2] not very likely though, considering you never need the array in the first place as you're just summing each item with the previous one, then summing every 100 item of that… anyway
- azth 11y agoCould you link to the code for `test.rs`?
- masklinn 11y agofn main() { let mut sum = 0i64; for _ in 0..200 { let items = (0..1000000).zip(1..1000000).map(|(a, b)| a + b); sum = items.enumerate().filter(|&(i, _)| i % 100 == 0).fold(0, |acc, (_, v)| acc + v); } println!("{}", sum); } (note that the addition part does some extra work since it iterates on the whole source instead of skipping ahead)