Why is My Nim Code Faster when Doing This

2023-08-08 Thread walkr
let s = "Thank you all for your inputs! (:" Run Since compiling modern programming languages involves so many moving parts, there's lots of useful information in here to learn from. I'm not going to be spending much more time on this microbenchmark, but it's good to know now

Why is My Nim Code Faster when Doing This

2023-08-08 Thread cblake
Surprised no one has yet mentioned: import std/algorithm proc main = var s = "Hello world" when defined(faster): reverse(s, 0, s.len - 1) for i in 1..200_000_000: reverse(s, 0, s.len - 1) main() Run and which side steps all the allocation stuff

Why is My Nim Code Faster when Doing This

2023-08-08 Thread demotomohiro
On my machine $ nim c -d:danger -d:lto --listcmd reverse.nim .. $ time ./reverse real0m31.493s user0m31.491s sys 0m0.001s $ nim c -d:danger -d:lto --listcmd -d:faster reverse.n

Why is My Nim Code Faster when Doing This

2023-08-07 Thread awr1
one of the things that is also less fortunate about this example is that there is a latent yet untaken opportunity for DCO to turn `reverse` into a noop the way it's used in the loop.

Why is My Nim Code Faster when Doing This

2023-08-07 Thread Isofruit
Very much seems like a compiler edge case. Here a benchy benchmark: import benchy proc reverse(s: string): string = result = s let size = s.len for (i, c) in s.pairs(): result[(size - i - 1)] = c let s = "Hello world" timeIt "With Disc

Why is My Nim Code Faster when Doing This

2023-08-07 Thread Yardanico
I feel like the difference can be explained by how the Nim allocator works (with it not releasing memory back to the OS) somehow, or by strings being CoW, because with `-d:useMalloc` there is no substantial performance difference between the two cases.

Why is My Nim Code Faster when Doing This

2023-08-07 Thread jasonfi
It looks like a weird compiler edge case. How much time do you want to spend on it?

Why is My Nim Code Faster when Doing This

2023-08-07 Thread Araq
Micro benchmarks are weird and not interesting. You cannot reverse a string in this way either, you have to use `unicode.reversed` because of Unicode.

Why is My Nim Code Faster when Doing This

2023-08-07 Thread treeform
Also I want to add, I think the reverse speed is probably trivial, what you are testing is allocating and deallocating a small string 200_000_000 times.

Why is My Nim Code Faster when Doing This

2023-08-07 Thread treeform
Testing speed is really hard. I recommend some thing like (benchy)[] rather then `time` utility. Also a super smart compiler can figure out that you are doing nothing and optimize your program out. What you are really testing is not what code is faster but ho

Why is My Nim Code Faster when Doing This

2023-08-07 Thread walkr
I was doing some testing, comparing Nim and Rust for some string operations, and I somehow managed to cut Nim's runtime in half by adding a call to my reverse function outside of the loop. proc reverse(s: string): string = result = s let size = s.len for (i,