r/programming • u/mtz94 • 23h ago
C vs C#: racing VLC's Gaussian blur
https://martinfinkel.com/blog/gaussian-blur-c-vs-csharp/25
u/yanitrix 16h ago
I love how multipurpose c# is. If you want to write something fast with memory safety not caring about performance you just do it. If you need something performance critical you can go low-level use things like stackalloc, spans, or even raw pointers and allocate memory yourself. If you want to ditch the runtime you can just use AOT. And then you can even write fucking frontend in it.
9
u/EliSka93 14h ago
Writing a frontend with C# Blazor WASM is so nice. It cuts down on about 98% of the JavaScript I'd have to write. There's barley any context switching.
9
u/Eisenfuss19 16h ago
AOT does come with a dotnet runtime embeded. It just skips saving the IL representation and compiles the IL at build time to machine code.
2
u/Saint_Nitouche 5h ago
Not to mention all the gamedev workloads where it's the most natural choice.
1
u/yanitrix 5h ago
but isn't it just unity?
3
u/Saint_Nitouche 5h ago
Monogame and Godot also let you write C#. Those three combined probably make up a decent proportion of all games on Steam. Terraria has made like over $100M and is written in Monogame (horrifying but true).
1
u/ziplock9000 4h ago
I moved back to monogame after 5-6 projects on Unity and I can tell you the project is far more stable and progressing faster. Unity is an illusion, a house of cards a lot of the time.
1
u/ziplock9000 4h ago
No, not by a long way. Even if it was Unity accounts for a huge amount of all games.
5
1
u/Dwedit 11h ago edited 11h ago
The traditional way to optimize a blur is to blur horizontally, then blur vertically. That way you avoid visiting a square region around every pixel.
Then if you want to optimize for memory use/memory bandwidth, you change the full-sized temporary buffer into a sliding window.
But things like blur filters are usually best done as a pixel shader rather than done on the CPU.
-12
u/nukethebees 15h ago
For everything else, the language you are most productive in, plus a good algorithm, will get you further.
I wouldn't make this the take-away when the author had to keep rewriting the implementation until it matched Clang.
The C# in code in the end is almost C. At that point, why not just write this in C and call it from C#?
12
u/mtz94 15h ago
When the last 10% don't matter, yes it's a good take away.
Because the point was to experiment how far I could take C#. Regardless, there are many valid reasons to write C# for perf sensitive code as well (team skills, interop with other dotnet libs, portability, etc.)
-8
u/nukethebees 15h ago
When the last 10% don't matter, yes it's a good take away.
It's not just the performance, it's also the development time. You essentially wrote a version in C and then used Claude to keep rewriting the C# until it matched the C. You could just have written the C version, called it in C#, and stopped there.
Because the point was to experiment how far I could take C#.
Any language that compiles to machine code can theoretically achieve the same performance. To take that argument to its logical extreme, languages with inline assembly are equally fast. We know this isn't true for the general case.
I'm sure it was fun to do but at the point where you're busting out
unsafeblocks, hand-writing intrinsics, and restructuring the code to get around the compiler, the language doesn't really matter anymore. It's not really C# as people know it any longer.10
u/mtz94 14h ago
Yes unsafe C# does not look like regular C#. That's ok.
I'm not saying all C software needs to be written in higher-level languages. I'm showing what C# can do with some optimization. That is all.
And your development time argument is no longer true with proper AI assisted programming.
3
u/yanitrix 5h ago
It's not really C# as people know it any longer.
It is. C# has many faces,
unsafecode is one of them.
49
u/Khavel_dev 17h ago
The gap between C and C# for this stuff has gotten absurdly small tbh. Between Span<T>, stackalloc, and System.Runtime.Intrinsics for SIMD, you can write C# that the JIT turns into basically the same machine code. .NET 9/10 have been quietly shipping perf wins that most people haven't noticed yet.