C vs. C#: racing VLC's Gaussian blur
71 points by martz 4 days ago | 17 comments

theanonymousone 39 minutes ago
> Two small edits close most of it: compute the loop bounds into locals before the loop,

I always assumed any decent runtime/compiler takes care of such optimisations, and usually leave such things to JDK in my Java code. Is that really not the case?

reply
RandomOnyx 20 minutes ago
Decent ones do. .NET's JIT has historically (before ryujit especially and tbh I haven't looked at .NET much in the past decade) not been decent, general statements about decent compilers never applied to it. It has been playing catch-up lately but apparently it still has a ways to go.
reply
teo_zero 3 hours ago
Nice article!

What surprises me is that the original VLC filter does not use SIMD. Sometimes I wonder why I spent my money on a "modern" CPU with a lot of new features that stay completely ignored by most software I run every day.

reply
fenwick67 3 hours ago
It surprises me that it runs on the CPU at all, honestly.
reply
ygra 3 hours ago
Probably a reflection of the age of that code along with the usefulness of the feature. If no one actually needs the video blurry, then it doesn't matter how efficient the filter is.
reply
martz 3 hours ago
[flagged]
reply
brookman64k 3 hours ago
I would have expected the filters to run as a shader on the GPU which would make them much more efficient. I remember implementing two-pass gaussian blur in GLSL almost 20 years ago.
reply
bschwindHN 3 hours ago
Yep if you want it done quickly, the GPU is the way to go. Especially once you get to 4K resolution and beyond.

See here:

https://aras-p.info/blog/2026/10/01/Fast-blur-with-animated-...

reply
anderskaseorg 3 hours ago
There are much faster algorithms available for Gaussian blur though, running in O(n) rather than O(nσ) for an n pixel image and radius σ. See https://www.ipol.im/pub/art/2013/87/article.pdf for a survey.
reply
schobi 2 hours ago
If we look at the example of the FFT blur filter, sure O(n) looks easier and independent of the blur size, but you have an overhead in needing FFT and iFFT. It can be faster for some sigmas, but not for all. So you need different variants depending on sigma and a smooth transition between them.

Then you can start approximating - for large sigmas just using every 10th pixel would work. Looks like MS Teams background blur is doing this, you get a blurry background with aliasing - yay!

Once you add SIMD variants and look at CPU caching behaviour, you build versions for different CPUs. Instead of filtering the whole image for one dimension you might finish blocks locally.

You've now got a dozen variants that need testing and maintenance on different architectures.

I'm reminded of my colleague preaching requirements engineering - what was the goal gain?

reply
anderskaseorg 39 minutes ago
FFT is O(n log n), not O(n). The better O(n) algorithms are the likes of Deriche, Vliet–Young–Verbeek, or Alvarez–Mazorra.
reply
hn_submit 2 hours ago
The C# version uses unsafe raw pointers which is practically the same as writing it in C. No surprise then that the outcomes are similar.
reply
maxlin 6 hours ago
Very interesting readup! I have some C# projects I've had in mind to try to compile to AOT. Quite different for complex programs of course, but the perf being very close to C is a great motivator.
reply
mihular 3 hours ago
Why would you want them in AOT, though? It's great for startup time and native interoperability, but it might be slower in general due to machine specific optimizations.
reply
mcraiha 3 hours ago
Because of Docker. You want the images to be as small as possible.
reply
littlecranky67 38 minutes ago
I would object, I want my containers to be as fast as possible and the JIT compiles for each CPU arch you run it on (supposedly optimally). Docker image size, who cares?
reply
grougnax 5 hours ago
[dead]
reply
Brian_K_White 4 hours ago
Most industries consider 8% a mountain. More than their entire profit margin.
reply
Dylan16807 4 hours ago
And 8% milk would taste bad and ruin most recipes.

I don't know why we're comparing completely different categories of number though. In a business context getting a project done a few percent faster, or more accurately one step done a few percent faster, barely affects revenue at all. And saving CPU time matters even less.

reply