I have written a differential evolution algorithm in cpp and Nim.
My cpp version is faster (6-8 times) and I was hoping I could get comparable performance with the Nim implementation. I need a hand.
Nim version: https://github.com/peheje/nim_genetic/blob/master/DifferentialEvolution/app.nim Compiling with nim compile -d:release app.nim
cpp version: https://github.com/peheje/differential_evolution_cpp/blob/master/differential_evolution/main.cpp Compiling with -Ofast and Link Time optimization: Monolithic in XCode.
When I run the Nim profiler it seem something called system.nim: genericReset is time consuming, what is this?
I have not used the random library. I think it is the strategy to make xoroshift unbiased with the while loop is slowing it (https://github.com/nim-lang/Nim/blob/2a8d2ad4cde1c942a9dbc44598f65d4acf1e0aa6/lib/pure/random.nim#L77) and my application don't care much about the bias introduced.
Okay. I noticed in another thread (https://forum.nim-lang.org/t/1268/1#7848) Araq telling to wrap the code in a main proc.
I did and now the speed is almost comparable to my cpp!
Nim: 0m0.197s cpp 0m0.159s
Impressive. I updated the master branch
Hi def You beat me to it!
Thank you very much for looking into it and replying. You are right!
I don't get quite the same performance boost as you do, cpp still seems faster but barely. Might be any number of reasons.
Do you have any more nice tricks for the nim code? Did you also wrap the constants and the xoroshift variables inside the main proc?
Yes, wrapped everything in main: https://gist.github.com/def-/4e4dea8f0f40ef7a77a2729148d8fe0e
You could try a more recent GCC version (7.3.1 here). nim -d:release --gcc.options.speed="-Ofast -flto -fno-strict-aliasing -ffast-math" --gcc.options.linker="-flto" c app but measure which actually make a difference, if any.
I get 0.08 - 0.12 s with nim --cc:clang -d:release --clang.options.speed="-Ofast -flto -fno-strict-aliasing -ffast-math" --clang.options.linker="-flto" c app using Clang 6.0.0.
Always use a main proc in Nim.
----
I noticed in another thread (https://forum.nim-lang.org/t/1268/1#7848) Araq telling to wrap the code in a main proc.
That thread is 3 years old. I've been told recently that using main is not required anymore, i.e. there shouldn't be speed/optimization differences if you use main or not.
Can somebody comment on this?
Thank you for the gist. You corrected some of my brainfarts as well.
Clang seems to be faster for me than gcc! Now faster than my cpp implementation. What configurations sets gcc as standard when compiling Nim?
My results: Clang: 0m0.144s GCC: 0m0.197s
I'm on a MBP 2013 with Core i7 I7-4850HQ.
clang --version: Apple LLVM version 9.1.0 (clang-902.0.39.1)
I've been told recently that using main is not required anymore, i.e. there shouldn't be speed/optimization differences if you use main or not. > Can somebody comment on this?
It's still required but we could make the compiler do this transformation for us. However, there are much more important issues left, as usual.