hi all
im relatively new to programming, mainly out of an interest in game development. i've only been learning since about a year plus, and started with c#, and have also recently picked up c++, even though it has been highly discouraged to do so. i still want to learn it anyway because so much stuff is built with c++ and also because i just want to. i like both languages and i think im somewhat aware of the controversy around these languages.
so im pretty much a newbie but from what i've been reading, nim sounds like a pretty nice language to pick up as well. i read a lot of good things about it and ive gone over a few tutorials. im going to start the exercism course on it soon i think.
ive a bit of an obsession with performance and cleanliness. especially the type of software im interested in working is performance-critical. i have a interest in writing low level stuff. nim seems ideal. but i wonder how c# stacks up to nim. maybe i should do more tests. but some test programs ive recently written, particularly string heavy ones, c# seems faster than nim by quite a bit. for example it seems string manipulation (like concatenation) is a lot faster in c# than in nim, and even way faster in c++
also this benchmarks website (admittedly i dont know a whole lot about benchmarks and how much i can understand from them) seems to place c# quite a bit ahead of nim in more tests than i would've believed based on what i've heard about nim's performance:
https://programming-language-benchmarks.vercel.app/nim-vs-csharp
but i do like how nim programs consistently have few lines of code than the same program in really most other languages which seems like a good thing
i dont know what timeout means in this context. does it mean the program failed to complete at all?
anyway long story short i was wondering why should a c# programmer, with all the recent performance improvements that come with .net 7, consider more seriously committing to nim?
I'm not aware of these benchmarks where C# has the lead but it's possible, beware though that usually the people who wrote the benchmarks might not be experts on Nim, esp when it comes to string handling. Also, once "copy on write" strings finally land in Nim naively written string handling code should become much faster.
Nim runs on many more platforms than C#, if that is not important for you, C# might be the better choice. But Nim also has a really nice macro system, the better syntax (subjective, but you seem to agree), and also the better type system.
I wrote about my experience with both recently:
https://mode80.github.io/7-langs-in-12-months.html
You might also like this: https://github.com/frol/completely-unscientific-benchmarks
i've only been learning since about a year plus, and started with c#, and have also recently picked up c++, even though it has been highly discouraged to do so. i still want to learn it anyway because so much stuff is built with c++ and also because i just want to. i like both languages and i think im somewhat aware of the controversy around these languages.
The risk is being frustrated and giving up. But doing something you're intrinsically curious and motivated about also give you strength to overcome the obstacles. Though don't hesitate to take a break if you're stuck, you can try a new language or a small project or do something else before coming back.
ive a bit of an obsession with performance and cleanliness. especially the type of software im interested in working is performance-critical. i have a interest in writing low level stuff. nim seems ideal. but i wonder how c# stacks up to nim
In my experience, performance reachable in C is easily reachable in Nim, by running c2nim on the code. Performance reachable in Assembly is reachable in Nim as well with inline assembly. This is something I did multiple times in high-performance computing and also high-speed cryptography:
and significantly more redable, see OpenSSL assembly codegen: https://github.com/openssl/openssl/blob/12ad22d/crypto/sha/asm/sha256-586.pl#L516-L653 and my code: https://github.com/mratsim/constantine/blob/1cb6c3d/constantine/hashes/sha256/sha256_x86_shaext.nim#L38-L130
but some test programs ive recently written, particularly string heavy ones,
Strings are very particular beasts. First of all the slowest language for string processing is C because it doesn't even store the length so all string processing starts by scanning the whole string to get it's length. Besides, naively using strings will allocate memory over and over for intermediate buffers. If performance critical instead you want to mutate the same buffer. The problem is the same in C++, Nim strings are the same as those in C++: a buffer, the length and the buffer max capacity. In garbage-collected languages though, the garbage collector can be highly tuned to string allocations and deallocations since it's pretty common cough Perl
anyway long story short i was wondering why should a c# programmer, with all the recent performance improvements that come with .net 7, consider more seriously committing to nim?
If you're a game developer, Unity, one of the major frameworks allows C# scripts so it's pretty compelling to stay there. If performance is so critical that you consider assembly, Nim is actually a viable alternative to just code selected hot paths in inline assembly. But if you use windows, there is no inline assembly so shrug. Now if you want to have fun, I personally find Nim enjoyable to code in.
No idea how learning C++ is "highly discouraged" lol
Before addressing your concerns I would say comparing to C# and C++ the biggest problem Nim has is that it's a smaller language. There are less and less mature libraries and frameworks, you will get less support for your problems, etc etc. This has not really been a problem for me and how I've been using the language, but I'm guessing it would be most people's biggest obstacle in using Nim. (In fact this is the reason for those timed out benchmarks, a pure Nim library for bigints is being used that isn't as fast or mature as other implementations)
If you are truly meticulous about performance I don't see why you would choose C# over Nim. Nim allows you to generate basically any C code with abstractions to make it palatable and I don't think there's much debate in how fast C is. But even in the pure language, reference types aren't the default, there's no hidden dynamic dispatch, you can tune runtime checks and memory management. Even if some benchmarks show bad results they will not always take full advantage of the language.
There are also additional upsides for game development. The compile times are good, you can compile to JS as well as C and C++, these backends also make the language very portable. But the biggest thing that ties everything together is the metaprogramming, for which Nim has basically no competition. Since it's not my job to sell Nim I won't continue much and others can look into these themselves
@Araq, is string "copy on write" coming with nim 2.0? If not, is it planned for a specific release?
It's not particularly hard to do but not scheduled either. PRs are welcome. :-)
thank you for your reply, and i am very impressed by your work and that of your fellow devs :)
c# i work with mainly because unity supports it and i think it's a fine language for someone still trying to find his way. outside of unity it's great for creating personal apps as well, though some .NET stuff has some issues (.NET MAUI isn't perfect atm, with some major memory leak issues) and apps tend to be quite big in size
c++ im definitely sticking with learning even if just for learning how lower level stuff works in the first place. c# doesnt satisfy my curiousity enough in that regard. it feels like im not really programming for some weird reason? it's more just sticking a few libraries together and calling some methods here and there. ive done the entire c# exercism course and ive written a few small apps for personal use but so far programming in general has not really satisfied me yet. it's more like im just learning c# just to use unity
i feel i'd also need to learn C or C++ in the first place to understand what im doing. it feels like most ppl who i see use nim kind of already have many many years of experience and are C masters whereas im not
i see that you have recently published a book called "Mastering Nim: A complete guide to the programming language" which seems great, i like learning from books, but unfortunately physical books are inconvenient for me, since i dont like sitting down, and reading physical books kind of necessitate that because you need a fixed source of light. i always like to walk around the house with my tablet instead. i was wondering if you were considering releasing a digital version in some way. i understand you may have piracy concerns which is completely understandable, i dont want to impose or anything like that. but it's just my vote in the bucket there :)
hi ppl thank you for your replies
ive spent the last weekend going through the Nim Official Tutorial and doing some exercises on Exercism and doing some other stuff
i really find that the language goals of efficiency, expressiveness and elegance have been well met and i want to work more with it. i think these are very fine points to design the language around. i find that Nim is easily one of if not the most elegant language there is. and i think that is the main thing for me that helps distinguish it from others. it is very pleasant to write and read nim code.
going back to c# today has been very rough. i suddenly realise that maybe im just bad at programming but i still dont really understand c# well at all after almost two years of trying to learn it now. im still not confident with it. i havent actually made anything useful with it at all. it is frustrating because c# has is constantly being promised by ppl as "simple" and "easy to learn" and i just do not agree with that at all. i see a common complaint with c++ is that it is a very big language with a lot of features and a lot of ways to do things.
well i want to say that c# is basically that as well and there seems to be a growing consensus on that. it's a massive language where i constantly find myself inadvertantly importing namespaces and classes with long verbose complicated names (due to mistyping and pressing enter) that i have no idea of what they do. it has so many keywords and a myriad ways of creating objects and so many ways of creating fields and so many ways of assigning variables that it is still so confusing to work with and i find code in it so hard to read. i still find it overwhelming and to try to finish my project (which is my first actual application at all) also because the framework that im using (a new one from microsoft) has many issues including a major memory leak problem). i find it so hard to just do simple things, constantly wondering whether what im doing is the 'proper' way of doing so
anyway i would personally not recommend ppl new to programming to pick up c# at all. only if you want to use unity game engine. other than i see no reason not recommend ppl to pick up nim. i think nim's main appeal is easily human readable code and it achieves that goal very well
With C# you have a ceiling of high-level code - a ceiling of abstraction, on the other side you have a floor you can't break - you cannot go too low-level either. In C# you will have to use reflection or annotations or something horrible as that, to go further, and it is really really really slow. One other downside of C# is quite slow compilation, and you have to bundle the entire runtime if you want to send your executable to a friend. The pros are good documentation, a stable, safe and nice language, compiler errors are searchable, haven't seen any compiler bugs, huge ecosystem of libraries and a large stdlib, kind of portable too, and speed is ok compared to scripting languages.
Nim is the best language, but you can port C# code over once you know enough
thank you for your reply and i agree. i think the level of abstraction in c# may be too high for my liking. i know that sounds like a weird thing to say but i cant describe it any other way
i think i may have been too hard on c# in my previous post. it's a really nice language for what it is but i think what i dont like is the object orientated mindset about it. the language, and all learning materials that teach you how to use it so far, teach/force you to think in a object oriented way and i've been constantly struggling with that. it seems to make things more complicated than they needed to be. it forces you to think about structuring/organising your entire project, and any future extensions to it, right from step 1, and thinking of every little thing as an object. it feels like im writing a ton of code without it actually doing anything.
some saying goes like 'premature optimisation is the root of all evil', well i think oop is basically premature obsession with code reuse and extensibility. it's not fun to work with. i can see how that makes more sense in an enterprise environment with big teams and long term maintenance. but i just want to learn to make something cool. i see now why oop is so frowned upon by some.
to the point, even in the Nim Official Tutorial (i think was part II) it mentions that it has support for minimal OOP, but it emphasises doing things in a procedural mindset. i think i need to start looking for more non-/anti-OOP style learning materials
I would have to disagree, OOP is very useful. ...
I have to disagree with that. Modules are much better than OOP. OOP only complicates things more than necessary. If you work in a big team, the first thing that you do is to create a structure in where people can work in different areas. Sometimes things break, that is Okay, don't do it regularly ;-)
And keep in mind the wise words of Edsger Dijkstra:
It's one of my pet theories that tutorial material for OOP languages, such as C#, end up spending a lot of time explaining how the OOP features work. This leads to the mistaken understanding by the learner that it is recommended to heavily use OOP. Over the last two decades we have learnt that OOP is not the be-all and and-all, and can often be left on the shelf or only lightly used. C++ in the nineties particularly suffered from this problem.
It's a well know maxim in system design to 'prefer composition over inheritance', I strongly recommend that you research and understand this idea.
I do agree with one of the other commenters that interfaces are a very valuable concept and should be recommended, together with dependency injection and test driven development.