Hi All,
Like some other people who recently posted here, I have been looking for a good modern C++ replacement. My search has not been as exhaustive as Marek's, but I did enjoy reading his opinions. I have been doing mainly Java for years, until I started looking at go about a year ago. However, I started running into some performance issues, such as significant speed increases when inlining core functions by hand. I even wrote a go pre-processor that inlines simple functions for you. This kind of optimization in nim is built in because I think you could do it with a template if you had to (right)?
I am also looking at Simple Direct media Layer, SDL, because it is very portable and gets you to the graphics hardware quickly. So, as an exercise to learn some nim, I wrote an SDL2 streaming textures demo in nim and go and compared performance on my modest linux laptop. I ran the development version and the “-d:release” version of the nim code. Go really doesn't have compiler options that are encouraged it seems, so there is only one flavor of go executable. Here are the results:
Program | Size | %CPU: | Mem virt | res | shr |
---|---|---|---|---|---|
nim development | 182040 | 73-75 | 282116 | 28352 | 12740 |
nim release | 81849 | 7.5-6.5 | 279908 | 28232 | 12648 |
golang | 2827703 | 13-14 | 366032 | 29996 | 14068 |
The nim executables are 15 and 34 times smaller than the go version. Wow! I guess shaking the tree really works. The program repeatedly streams a bit pattern to a graphics card texture as fast as possible but has to wait for the graphics card to load and process the data, so the percent CPU is telling us how much CPU the program needs to keep up with the graphics card. The development version with all of the asserts (I assert check every error possible) is the slowest, but the release version is almost twice as fast as the go version. Memory usage is about 10% less with the nim versions.
I also noticed that if I sent text to stdout in a terminal while running, the go version would inevitably stutter due to some sort of buffering and garbage collection, while the nim version always runs smoothly. This supports the notion I have read on other blogs that go is not a good choice for embedded systems, games, and real time applications because the program cannot avoid or control the gc. Nim on the other hand is lightweight and runs more smoothly, at least in this example.
The source code for the examples are here: http://github.com/srwiley/nim-sdl2-examples. There is also an example of simple and custom SDL2 message boxes in nim, which some people might find useful since there are not a lot of examples out there on the web as far as I know. Since I am new to nim, any comments on improving my syntax would be appreciated. I am sure I will be writing more nim in the future.
Good job nim team and thanks!
Welcome to Nim! :)
You might get even better performance if you put the code in the block outerloop in a main procedure.