314

Or is it now the other way around?

From what I've heard there are some areas in which C# proves to be faster than C++, but I've never had the guts to test it by myself.

Thought any of you could explain these differences in detail or point me to the right place for information on this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Trap
  • 12,050
  • 15
  • 55
  • 67
  • 9
    Protected, to prevent any more random benchmarks from being posted. If you think you can make your case, you will need 10 rep to do so. – Robert Harvey May 10 '11 at 18:13
  • 1
    It's almost a moot question, given that we live in an age in which IL can be converted to CPP and optimized from there: https://docs.unity3d.com/Manual/IL2CPP.html – pixelpax Apr 02 '18 at 17:22
  • A language that checks for out of range array access will never outperform one that doesn't. – Seva Alekseyev Mar 03 '20 at 22:02
  • @SevaAlekseyev It's not the language who does this but the compiler. One of the reasons C++ is that fast (apart from the obvious ones) is that C++ compilers have been around for past 35 years (if not more). There's nothing that prevents C# compilers to get better over time. For the case you mention, please read this https://stackoverflow.com/questions/16713076/array-bounds-check-efficiency-in-net-4-and-above – Trap Mar 04 '20 at 10:42

29 Answers29

413

There is no strict reason why a bytecode based language like C# or Java that has a JIT cannot be as fast as C++ code. However C++ code used to be significantly faster for a long time, and also today still is in many cases. This is mainly due to the more advanced JIT optimizations being complicated to implement, and the really cool ones are only arriving just now.

So C++ is faster, in many cases. But this is only part of the answer. The cases where C++ is actually faster, are highly optimized programs, where expert programmers thoroughly optimized the hell out of the code. This is not only very time consuming (and thus expensive), but also commonly leads to errors due to over-optimizations.

On the other hand, code in interpreted languages gets faster in later versions of the runtime (.NET CLR or Java VM), without you doing anything. And there are a lot of useful optimizations JIT compilers can do that are simply impossible in languages with pointers. Also, some argue that garbage collection should generally be as fast or faster as manual memory management, and in many cases it is. You can generally implement and achieve all of this in C++ or C, but it's going to be much more complicated and error prone.

As Donald Knuth said, "premature optimization is the root of all evil". If you really know for sure that your application will mostly consist of very performance critical arithmetic, and that it will be the bottleneck, and it's certainly going to be faster in C++, and you're sure that C++ won't conflict with your other requirements, go for C++. In any other case, concentrate on first implementing your application correctly in whatever language suits you best, then find performance bottlenecks if it runs too slow, and then think about how to optimize the code. In the worst case, you might need to call out to C code through a foreign function interface, so you'll still have the ability to write critical parts in lower level language.

Keep in mind that it's relatively easy to optimize a correct program, but much harder to correct an optimized program.

Giving actual percentages of speed advantages is impossible, it largely depends on your code. In many cases, the programming language implementation isn't even the bottleneck. Take the benchmarks at http://benchmarksgame.alioth.debian.org/ with a great deal of scepticism, as these largely test arithmetic code, which is most likely not similar to your code at all.

igouy
  • 2,547
  • 17
  • 16
Martin Probst
  • 9,497
  • 6
  • 31
  • 33
  • 112
    code in interpreted languages gets faster in later versions of the runtime As code compiled by a better version of the compiler will also get faster. – Martin York Sep 26 '08 at 09:44
  • 3
    Note that, if sb has to code performance critical arithmetic, it's best to code it in Fortran, otherwise provide hints to the C++ compiler that the data won't be aliased by other pointers. – tzot Sep 26 '08 at 09:50
  • 55
    In fact there is at least one reason: JIT needs to be fast, and cannot afford to spend time on various advanced optimizations available to a C++ compiler. – Nemanja Trifunovic Sep 26 '08 at 14:04
  • 2
    @Nemanja Trifunovic: depends on your scenario. In server applications, JIT doesn't really need to be fast - you can amortize the cost over time very well, and perform incremental enhancements on the code. – Martin Probst Sep 29 '08 at 14:18
  • 2
    I like C++, but that's probably because I program games where almost everything is insanely math heavy (physics, collison, weighted mesh deformation - stuff like that.) – Cristián Romo Oct 08 '08 at 16:54
  • 71
    "but also commonly leads to errors due to over-optimizations." [citation desperately needed]. I work at a national lab, and we optimize the hell out of our code. This does not commonly result in buggy code. – Todd Gamblin Dec 11 '08 at 13:52
  • @martinprobst "with a great deal of scepticism" - no, not scepticism. The appropriate attitude is curiosity - see the benchmarks game FAQ "Flawed Benchmarks". – igouy Feb 21 '09 at 17:41
  • 42
    "It's relatively easy to optimize a correct program, but much harder to correct an optimized program." – gradbot Jul 17 '10 at 16:33
  • 9
    @Matin York: But the new JIT can also execute older assemblies faster, the new compiler is useless without a new compile ;) – Aidiakapi Mar 03 '11 at 21:48
  • 2
    C# is programmed in C++, so I don't see how it could possibly be faster any faster than C++. At best C# can only be as fast as C++. Then again, .NET has a managed heap, managed thread pool, etc. that may make your code faster than a C++ program that does not have these managers - but these are features of .NET and not the C# programming lanugage itself. –  Jan 21 '13 at 14:38
  • ..to clarify: the C# compiler and the .NET CLR & CLI is programmed in C++. –  Jan 21 '13 at 14:47
  • 22
    Inge: not sure you're on the right track there. Yes, C# is implemented in another language, but the JIT compiler is generating machine code, so it's not an interpreted language. Thus it is not limited by its C++ implementation. I'm not quire sure why you think adding some manager to something inherently makes it faster. – Martin Probst Jan 26 '13 at 12:59
  • 1
    @NemanjaTrifunovic: "JIT needs to be fast, and cannot afford to spend time on various advanced optimizations available to a C++ compiler". That is often claimed but I have never seen any evidence for it. Can you cite some concrete examples of such optimizations? – J D Nov 02 '13 at 16:44
  • 3
    @Aidiakapi JIT means you have a compiler at runtime environment. There's no reason not to recompile C++ program with a new compiler. – eonil May 09 '14 at 10:23
  • 7
    @Eonil Except that it requires redeployment? For server systems, sure go ahead and recompile software with more modern compilers that have more advanced optimizations. But for the consumer market you usually can't do that until you ship a new version. With .NET a program compiled 8 years ago, may suddenly become faster because of an optimization in a new .NET Framework. – Aidiakapi May 09 '14 at 11:35
  • What do you think about the new C# "roslyn" compiler? I've heard it really speeds up the code significantly – Arsalan Ahmad Jun 16 '14 at 05:35
  • @ArsalanDotMe Roslyn is a compiler from C# to bytecode. There's very few optimizations done at this stage, since aggressively optimizing at that point would prevent more advanced optimizations at the JIT time. However, you're probably talking about RyuJIT, the new JIT compiler, which does have several major performance improvements. – Aidiakapi Jul 13 '15 at 20:25
  • 2
    actually the main point is that certain things cannot be done effectively in C# but can be done effectively in C++ e.g. AMP Maybe it is just temporary, but currently it is like that. – AndersK Dec 29 '15 at 01:08
  • [Donald E. Knuth, 2008-04-25] "X" can produce thousands of binaries, tuned perfectly to the configurations of individual users, whereas "Y" usually will exist in only a few versions. A generic binary executable file must include things like inefficient "sync" instructions that are totally inappropriate for many installations; such wastage goes away when the source code is highly configurable. This should be a huge win for "X". – Johan Boulé May 07 '16 at 22:56
  • 1
    I would say that it's more than bytecode vs native, it's also about the language itself, if the language is not expressive enough (and I think both C# and Java fit in this category) to express things efficiently for the backend, you're going to lose regardless whether your native or bytecode/IL etc, native languages can be slow too; I personally believe bytecode gets more merits that it should, and for the most part everything should be native, native can be usable too; it's also about how far do you need to go, do you really need it all up to last cycle? sometimes you do, most times you don't –  Jul 02 '16 at 07:20
  • @AndersK. This hasn't been true even when you posted that. It's not commonly used, but .NET has had support for AMP for a while, and AFAIK JVM has it too. Don't forget that C# can deal with unmanaged memory and pointers just fine - it's just a bit more work than in C++ (a bit similar to C, in fact). If you want managed code and C++, Managed C++ gives you both at the same time (though obviously, the native part of code is not multi-platform). And .NET makes interop extremely easy, so you can always drop to native C++ if you need to. Or VB6, whatever floats your boat. – Luaan Jul 14 '16 at 08:02
  • @Luuan I think I saw that in some old interview in relation to the "Native C++" initiative, don't remember exactly who or when so you are probably right. – AndersK Jul 14 '16 at 11:49
  • 2
    I don't think that code generated by a C++ compiler is necessarily faster (or slower) than the code generated by a C# compiler. I think C++ gives much more control over the performance characteristics of your code than C# does. – Ferruccio Sep 08 '16 at 20:20
  • When you mentioned this: "And there are a lot of useful optimizations JIT compilers can do that are simply impossible in languages with pointers," were you referencing aliasing-related optimizations? – Jose Fernando Lopez Fernandez Jun 29 '17 at 06:14
  • 4
    *"There is no strict reason why a bytecode based language like C# or Java that has a JIT cannot be as fast as C++ code."* Sure there is. For one thing, JIT compilers have more limited time in which to compile the code than AoT compilers. That's enough to put them at a disadvantage. – user541686 Mar 21 '18 at 22:27
  • "premature optimization is the root of all evil", this old quote doesn't hold in practice. You need to design for performance from the get go. Always been this belief that the JIT will outperform statically compiled languages, but in practice it never seem to quite happen, for logical reasons ( abstracting away memory management mostly ). Besides, the presumed benefits of a JIT is based on the fact that there's an large number of potential CPUs out there to optimize for, which in practice definitely isn't the case. – Ylisar Jun 07 '18 at 13:04
  • "Premature optimization is the root of all evil" is commonly attributed to Donald Knuth, but it was actually Tony Hoare (inventor of quicksort) who made this statement. – Sturla Molden May 07 '20 at 20:29
  • Your arguments are way too generic and mostly irrelevant. You have no effort to push into writing C++ code in standard uses to get a program running way too fast with few memory. And if you need to optimize C++ code, you have direct access to a vast array of optimizations that will be hard to beat by other languages; in the best case, just mimicking. The compiler does a gigantic effort because it has time to do so, inter and intra modules. And it's very, very strong at enabling zero-cost abstractions due to all what is known to be static at compile-time/ – J. Bailleul Mar 11 '21 at 23:39
140

I'm going to start by disagreeing with part of the accepted (and well-upvoted) answer to this question by stating:

There are actually plenty of reasons why JITted code will run slower than a properly optimized C++ (or other language without runtime overhead) program including:

  • compute cycles spent on JITting code at runtime are by definition unavailable for use in program execution.

  • any hot paths in the JITter will be competing with your code for instruction and data cache in the CPU. We know that cache dominates when it comes to performance and native languages like C++ do not have this type of contention, by design.

  • a run-time optimizer's time budget is necessarily much more constrained than that of a compile-time optimizer's (as another commenter pointed out)

Bottom line: Ultimately, you will almost certainly be able to create a faster implementation in C++ than you could in C#.

Now, with that said, how much faster really isn't quantifiable, as there are too many variables: the task, problem domain, hardware, quality of implementations, and many other factors. You'll have run tests on your scenario to determine the the difference in performance, and then decide whether it is worth the the additional effort and complexity.

This is a very long and complex topic, but I feel it's worth mentioning for the sake of completeness that C#'s runtime optimizer is excellent, and is able to perform certain dynamic optimizations at runtime that are simply not available to C++ with its compile-time (static) optimizer. Even with this, the advantage is still typically deeply in the native application's court, but the dynamic optimizer is the reason for the "almost certainly" qualifier given above.

--

In terms of relative performance, I was also disturbed by the figures and discussions I saw in some other answers, so I thought I'd chime in and at the same time, provide some support for the statements I've made above.

A huge part of the problem with those benchmarks is you can't write C++ code as if you were writing C# and expect to get representative results (eg. performing thousands of memory allocations in C++ is going to give you terrible numbers.)

Instead, I wrote slightly more idiomatic C++ code and compared against the C# code @Wiory provided. The two major changes I made to the C++ code were:

  1. used vector::reserve()

  2. flattened the 2d array to 1d to achieve better cache locality (contiguous block)

C# (.NET 4.6.1)

private static void TestArray()
{
    const int rows = 5000;
    const int columns = 9000;
    DateTime t1 = System.DateTime.Now;
    double[][] arr = new double[rows][];
    for (int i = 0; i < rows; i++)
        arr[i] = new double[columns];
    DateTime t2 = System.DateTime.Now;

    Console.WriteLine(t2 - t1);

    t1 = System.DateTime.Now;
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < columns; j++)
            arr[i][j] = i;
    t2 = System.DateTime.Now;

    Console.WriteLine(t2 - t1);
}

Run time (Release): Init: 124ms, Fill: 165ms

C++14 (Clang v3.8/C2)

#include <iostream>
#include <vector>

auto TestSuite::ColMajorArray()
{
    constexpr size_t ROWS = 5000;
    constexpr size_t COLS = 9000;

    auto initStart = std::chrono::steady_clock::now();

    auto arr = std::vector<double>();
    arr.reserve(ROWS * COLS);

    auto initFinish = std::chrono::steady_clock::now();
    auto initTime = std::chrono::duration_cast<std::chrono::microseconds>(initFinish - initStart);

    auto fillStart = std::chrono::steady_clock::now();

    for(auto i = 0, r = 0; r < ROWS; ++r)
    {
        for (auto c = 0; c < COLS; ++c)
        {
            arr[i++] = static_cast<double>(r * c);
        }
    }

    auto fillFinish = std::chrono::steady_clock::now();
    auto fillTime = std::chrono::duration_cast<std::chrono::milliseconds>(fillFinish - fillStart);

    return std::make_pair(initTime, fillTime);
}

Run time (Release): Init: 398µs (yes, that's microseconds), Fill: 152ms

Total Run times: C#: 289ms, C++ 152ms (roughly 90% faster)

Observations

  • Changing the C# implementation to the same 1d array implementation yielded Init: 40ms, Fill: 171ms, Total: 211ms (C++ was still almost 40% faster).

  • It is much harder to design and write "fast" code in C++ than it is to write "regular" code in either language.

  • It's (perhaps) astonishingly easy to get poor performance in C++; we saw that with unreserved vectors performance. And there are lots of pitfalls like this.

  • C#'s performance is rather amazing when you consider all that is going on at runtime. And that performance is comparatively easy to access.

  • More anecdotal data comparing the performance of C++ and C#: https://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=gpp&lang2=csharpcore

The bottom line is that C++ gives you much more control over performance. Do you want to use a pointer? A reference? Stack memory? Heap? Dynamic polymorphism or eliminate the runtime overhead of a vtable with static polymorphism (via templates/CRTP)? In C++ you have to... er, get to make all these choices (and more) yourself, ideally so that your solution best addresses the problem you're tackling.

Ask yourself if you actually want or need that control, because even for the trivial example above, you can see that although there is a significant improvement in performance, it requires a deeper investment to access.

U007D
  • 5,772
  • 2
  • 38
  • 44
  • 18
    @Quonux thank you for the comment. Of course this is not a "real program". The point of the benchmarks was refactor a C# benchmark offered elsewhere on this page as evidence that JITted code is somehow faster than native--it's not, and the benchmark was potentially misleading to new people. – U007D Mar 09 '17 at 00:44
  • 10
    @Quonux why do you have to write like that? It's people like you that makes me dislike stackoverflow. – Markus Knappen Johansson Jul 20 '17 at 21:03
  • 5
    @MarkusKnappenJohansson I had a bad day ;) , I'm just a human too, removed my downvote, yet my opinion still applies. Oh please don't dislike SO just because there are some "stupid" people :) . Have a nice one. – Quonux Jul 22 '17 at 17:17
  • 14
    COMPLETELY MISLEADING BENCHMARK. In C++ version you are simply reserving part of memory (and then marveling how that operation takes microseconds to execute). In C# version you are creating 5000 ARRAYS (instantiating objects in memory). C++ is faster than C#... but difference is nowhere near 40%... right now it's more in range of <10%. What your example illustrates is that programmers should stick with language of their choice (and from your profile it's obvious that you are career C++ programmer). In C# you can do 2D array `int[,]`... following up with example. – nikib3ro Dec 27 '17 at 17:52
  • 3
    So, I've redone C# example on my machine to use `double[,]` which is closer to what is used in benchmark. It is still not perfect comparison since almost anything in C# is object that gets initialized in memory (and there is overhead there). Results: initialization took 3 ms and execution took 95 ms. So I could now declare that C# is 40% FASTER than C++. Which is obviously not true. Maybe my machine is faster than @U007D. Maybe I forget to set Release mode (then my code takes >300ms to execute). Conclusion: speed of compiled code is equal. How well you've wrote the code is way bigger factor. – nikib3ro Dec 27 '17 at 18:04
  • 4
    From what I can tell, the code in your C++ example is literally just allocating the memory ahead of time. The PROPER C# implementation would simply write 'List arrs = new List(ROWS*COLS)' which allocates the memory required to index a 2 dimensional array in 1 dimensional format (eg, what you did in C++). There's absolutely no reason to allocate a 2-dimensional array and manually flatten it -- the massive amount of iterations in your pre-test is the cause of the shitty performance. I imagine the overhead would still be more in C#, but not by a considerable amount. – Jax Jul 07 '18 at 01:42
  • 2
    Very inneficient c# implementation as others have stated. Also, time was measured by subtracting DateTime objects which is not the correct usage (Not enough precision, heavier than Stopwatch). This post just proves that people should prefer the language that they are experienced with as opposed to online benchmarks. – Orestis P. Oct 03 '18 at 09:21
  • FYI, for those commenting on the quality of C# implementation, please be aware that I did not write the C# benchmarks. As I have stated, they were posted by another person on this page as evidence that C# was faster than C++, which, as I have argued, is generally false. – U007D Jul 17 '19 at 15:46
  • 1
    @JDSweetBeat, you may have missed the comparison of a comparable C# implementation I provided. Please see the first bullet under "Observations", above. – U007D Jul 17 '19 at 15:48
  • 1
    @U007D Thanks for pointing that out, I did indeed overlook that bulletpoint :-) – Jax Jul 25 '19 at 02:12
  • 1
    There is no fundamental reason why JIT solutions should be slower than precompiled code, if you disregard warm-up time (which may actually happen before the program is shipped). In fact, mathematically, the opposite is true: JITs have the option of optimizing further at runtime which once-compilers like C++ don't have. What we currently have available is another question, but long-term, JITs will basically win the performance race. – Stefan Reich Jan 18 '20 at 14:02
  • Simple for loop prove nothing because c# would have optimized lots of stuff to make it faster – Lokanath Jan 29 '20 at 04:41
  • It's true that c++ gives you so many tools to eek out the latest perf, but that all takes up space in your head. Those technical hardware level decisions break the abstraction of how you want to reason about your algorithms. C# alllows you to reason in a cleaner more abstracted environment, and by using that you specify more what needs to be done instead of spelling out how. This allows the compiler to rewrite to equivalent and faster machine code. – gjvdkamp Nov 19 '20 at 08:58
  • I would be interested to see the results of this where the C++ implementation was closer to the C# implementation. In other words: Allocating a `vector>` structure, where `Reserve` is used to reserve space for BOTH dimensions of the arrays. – FreelanceConsultant Mar 09 '22 at 10:23
  • 3
    Fairly sure what you're doing in the C++ example is illegal (almost certain to work, but illegal). You call `arr.reserve(ROWS * COLS);`, which makes it possible to `push_back` `ROWS * COLS` times without triggering reallocation. But instead of repeated `push_back`, you manually assign to progressively increasing indices of `arr`. This works in practice, because the memory is there and `operator[]` isn't doing bounds-checks, but it's undefined behavior (the size of the `vector` remains `0`, and you're accessing a non-existent element). – ShadowRanger Apr 11 '22 at 20:01
  • Nice catch, @ShadowRanger, I think you are probably correct in this. – U007D Apr 12 '22 at 19:23
  • 1
    Here's a more interesting comparison in my opinion. Using `std::array`, was able to get much better performance (~150ns total runtime) https://godbolt.org/z/arhsbrW4o. Was curious if C# even had language constructs to express the equivalent stack-allocated memory, and it does, using [`stackalloc`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/stackalloc), but in practice it seems this is too much for C# to handle https://dotnetfiddle.net/UtRP9d. – Patrick Roberts Jul 30 '22 at 10:32
98

It's five oranges faster. Or rather: there can be no (correct) blanket answer. C++ is a statically compiled language (but then, there's profile guided optimization, too), C# runs aided by a JIT compiler. There are so many differences that questions like “how much faster” cannot be answered, not even by giving orders of magnitude.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 223
    Have you got any evidence to support your outrageous five oranges claim? My experiments all point to 2 oranges at most, with a 3 mango improvement when doing template metaprogramming. – Alex Jan 02 '10 at 08:59
  • 2
    Agreed, I am getting 2 oranges as well based on my calculations, I am not seeing any mangoes though. – Sushant Aug 24 '22 at 18:45
67

In my experience (and I have worked a lot with both languages), the main problem with C# compared to C++ is high memory consumption, and I have not found a good way to control it. It was the memory consumption that would eventually slow down .NET software.

Another factor is that JIT compiler cannot afford too much time to do advanced optimizations, because it runs at runtime, and the end user would notice it if it takes too much time. On the other hand, a C++ compiler has all the time it needs to do optimizations at compile time. This factor is much less significant than memory consumption, IMHO.

Nemanja Trifunovic
  • 24,346
  • 3
  • 50
  • 88
  • 6
    In one project at work we had to mine gargantuan amounts of data, including holding many GB in memory simultaneously and performing expensive calculations on all of it -- this required precise control of all allocations, C++ was pretty much the only choice. +1 for C++. On the other hand, that was just one project, we spent most of our time writing systems which interacted with slow simulators, and debugging could be a nightmare, so I wished we could have used a programmer-time-optimizing language for all that other stuff. – Bogatyr Sep 09 '11 at 10:24
  • You can force the managed heap to dispose objects. See http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx for more info. –  Jan 21 '13 at 14:49
  • 7
    @IngeHenriksen: I am well aware of the Dispose pattern, but it does not help with the managed memory at all. – Nemanja Trifunovic Jan 21 '13 at 18:21
  • 10
    @IngeHenriksen disposing it only ensures that the Dispose method has been called. Disposal never frees garbage collected memory. The Dispose method is only intended for cleaning up unmanaged resources like file handles and has nothing to do with memory management. – doug65536 Jan 29 '13 at 17:52
  • 1
    @NemanjaTrifunovic: "JIT compiler cannot afford too much time to do advanced optimizations". Can you cite some optimizations that are not done by JITs because they would take too long? – J D Nov 02 '13 at 16:53
  • 1
    But also memory cheap these days, is it a bigger problem to add 32GB, or instead let someone code a few month's more in c++ (large project), as compared to c#. – Peter Apr 13 '16 at 12:29
  • 6
    @user3800527: Even if adding RAM was always feasible (and it is not - imagine Microsoft adding RAM to each MS Office user) that will not solve the problem. Memory is hierarchical and a C# program will have many more cache misses than a C++ one. – Nemanja Trifunovic Apr 13 '16 at 12:58
  • @doug65536: It sounds like Dispose does have something to do with memory management... When you "clean up unmanaged resources", the memory that contained those resources is freed up, right? – Joanna Marietti Aug 31 '16 at 03:24
  • 1
    @JoannaMarietti Well, yes, but eventually those resources will be freed by the finalizer even if you forget to call `Dispose`. Forgetting to immediately dispose a file opened exclusively could be a major issue, it might block it from being opened elsewhere for an unreasonable amount of time. You might also accidentally leave file locks held. Leaks are still possible with a garbage collector, if you accidentally keep reachable references unnecessarily. – doug65536 Sep 02 '16 at 19:06
  • 2
    Thank you, someone who actually provides an answer rather than just saying "oh it depends... blah". – sebjwallace Nov 29 '17 at 19:15
37

One particular scenario where C++ still has the upper hand (and will, for years to come) occurs when polymorphic decisions can be predetermined at compile time.

Generally, encapsulation and deferred decision-making is a good thing because it makes the code more dynamic, easier to adapt to changing requirements and easier to use as a framework. This is why object oriented programming in C# is very productive and it can be generalized under the term “generalization”. Unfortunately, this particular kind of generalization comes at a cost at run-time.

Usually, this cost is non-substantial but there are applications where the overhead of virtual method calls and object creation can make a difference (especially since virtual methods prevent other optimizations such as method call inlining). This is where C++ has a huge advantage because you can use templates to achieve a different kind of generalization which has no impact on runtime but isn't necessarily any less polymorphic than OOP. In fact, all of the mechanisms that constitute OOP can be modelled using only template techniques and compile-time resolution.

In such cases (and admittedly, they're often restricted to special problem domains), C++ wins against C# and comparable languages.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 6
    Actually, Java VMs (and probably .NET) go to great lengths to avoid dynamic dispatch. Basically, if there is a way to avoid polymorphims, you can be pretty sure your VM will do it. – Martin Probst Sep 29 '08 at 14:21
  • 2
    I'm aware of the VMs' abilities. However, this goes much farther. The point is that template C++ codes *do* use “dynamic” dispatching, or rather, something analogous. – Konrad Rudolph Oct 02 '08 at 15:50
  • 3
    +1 I always have trouble explaining this to my C# colleagues who know little C++ in a way that would enable them to appreciate the significance. You've explained it rather nicely. – Roman Starkov Sep 15 '10 at 00:33
  • 1
    But when combing code from multiple owners I believe it is still true that template instantiations are very hard to share across module boundaries. I'm talking about sharing common code like List or vector across many modules in an application. So for composable systems (many modules, many owners) runtimes like the CLR start to make up for their fixed overhead by reducing thrashing of the CPU cache with many copies of the same template instantiations. I think over time the C++ performance lead will shrink until only niche libraries and untyped C libraries remain. – yzorg Sep 16 '10 at 09:21
  • 9
    @crtracy: you are making your bet without high-performance computing applications. Consider weather forecasting, bioinformatics and numeric simulations. The performance lead of C++ in these areas will *not* shrink, because no other code can achieve comparable performance at the same level of high abstraction. – Konrad Rudolph Sep 16 '10 at 13:24
  • C# has native code compilation of run-time generated code which is a more general solution to this problem. For example, regular expressions are compiled to machine code in C# whereas C++ resorts to an interpreter. You cannot use templates to do that in C++ if the regular expression is only available at run-time. So, in the general case, C# is orders of magnitude faster than C++ in the context of metaprogramming. – J D Nov 02 '13 at 17:02
  • 2
    @Jon Use a better regex engine in C++ then (there might not be one; but that’s not a fundamental restriction – but actually there *are* such engines, e.g. Boost.Xpressive). Your second statement is laughable. – Konrad Rudolph Nov 04 '13 at 10:57
  • You can use a library like LLVM to dynamically generate native code in C++ if you want to. There's no language restriction against it. It would be a legal implementation of std::regex. – Puppy Nov 04 '13 at 11:01
  • 3
    @JonHarrop Incidentally, to avoid misunderstanding, I concede that C# metaprogramming is *more powerful* because you can use it *at runtime*. I’m taking issue with your “orders of magnitude faster” claim which is flat out wrong. – Konrad Rudolph Nov 04 '13 at 11:05
  • @DeadMG: Yes, of course. You can solve any problem quickly in any language by generating custom machine code. – J D Nov 05 '13 at 09:22
  • @KonradRudolph: "I’m taking issue with your “orders of magnitude faster” claim which is flat out wrong". How big do you think the performance difference between an interpreter and the code generated by an optimizing compiler is? I've seen Mathematica run 100,000x slower than C... – J D Nov 05 '13 at 13:20
  • 5
    @Jon Apples and oranges. Your specific claim was “C# is orders of magnitude faster than C++ in the context of metaprogramming”, not “using precompiled code is orders of magnitude faster than interpreted code”. While we’re at it, your claim that runtime code generation is “more general” than compile-time code generation is also clearly wrong – they both have strengths and weaknesses. Compile-time code generation uses the type system to provide static type safety – runtime code generation cannot do that (it *can* provide strong type safety, but not *static* type safety). – Konrad Rudolph Nov 05 '13 at 15:10
  • @KonradRudolph: "your claim that runtime code generation is “more general” than compile-time code generation is also clearly wrong". Run-time codegen can handle both statically- and dynamically-available programs whereas compile-time can only handle statically-available programs. "runtime code generation cannot do that". MetaOCaml is a counter example. – J D Nov 05 '13 at 18:27
  • 1
    @Jon That’s all nice and well but completely academic. Neither C++ nor C# can do this. This whole discussion is about C# and C++. – Konrad Rudolph Nov 05 '13 at 18:55
  • @KonradRudolph: Perhaps you could comment on the 13x performance difference I've described here: http://stackoverflow.com/questions/19798653/c-vs-net-regex-performance – J D Nov 05 '13 at 20:48
  • actually C# does include ways to inline your functions using System.Runtime.CompilerServices; ... [MethodImpl(MethodImplOptions.AggressiveInlining)] void MyMethod(...) – Peter Apr 12 '16 at 07:05
  • 2
    @user3800527 Nobody disputes that, but I don't know a single implementation of .net which could use that to inline, say, calls to an `IComparer` inside the loop of a sorting algorithm. C++ can do that. – Konrad Rudolph Apr 12 '16 at 18:49
  • you place it above the function your calling, you can do it multiple times, so a function can use a sub-funtion, and the subfuction will be placed inside your main function (when compiled) (ea no jumps or calls); but you m ight need to write your own sorting mechanisms (and maybe even paralize them) – Peter Apr 13 '16 at 12:25
  • 1
    @user3800527 That won’t work if the sorting algorithm is generic and can be called with distinct `IComparer` types. That’s the whole problem here, and which is solved by C++’ compile-time generics (= templates). – Konrad Rudolph Apr 13 '16 at 14:37
  • not exactly sure what you have in mind here, maybe put your data in a struct, and your functions inside a class so you could do without object-functions. (but maybe i need to see the problem you have) structs arrays lists hashtables and objects classes are all design and performance related choises, but not always properly chosen. – Peter Apr 14 '16 at 07:10
  • 6
    @user3800527 I think you’re missing the whole point of this answer. Of course you can work around this by breaking encapsulation and dropping down to low-level structures — you can write assembly in (most) any language. The thing that makes C++ (almost) unique, and uniquely suited for high-performance programming, is that you can build *high-level* abstractions that come at *no* runtime cost. So you don’t need to write assembly-like code in C++ to get premium performance: a well-written `sort(arr, generic_comparer)` will be as efficient as a hand-written loop in C++. It never will be in C#. – Konrad Rudolph Apr 14 '16 at 15:07
  • 1
    Yes, C++ can do this, and it's a great thing. But don't think that .NET (or JVM) can't - as far as I know, right now, you're right that the support isn't there. But in both, you already have the JIT compiler getting around virtual method calls if you mostly call the same method. Expanding this to allow *inlining* the virtual method bodies is obviously possible - though I wouldn't hold my breath, since it isn't important for most projects, and the workaround is very simple. Instead, we can expect the higher level abstractions to get better - e.g. smarter anonymous delegates. – Luaan Jul 14 '16 at 08:11
  • 3
    @Luaan No, it really isn’t possible. What C++ can do and other languages can’t is that *even if the outer function isn’t inlined*, the inner function will. For instance, imagine a `Sort` method that accepts a `comparator` argument. In C++, this `comparator` can be inlined even when `Sort` won’t be. In the languages you listed, this is fundamentally *not* possible, and this is design restriction, not a technical one that might vanish in the future. – Konrad Rudolph Jul 14 '16 at 10:25
  • @KonradRudolph You underestimate the fact that the runtime is free to optimize whatever it wishes as long as the executing program can't notice the difference (on a single thread). It's a perfectly valid optimization to inline the inner function, though it would certainly require a much smarter JIT than .NET has right now. It would happen at runtime, yes, but .NET has enough metadata to make it possible. And due to the runtime compiler features, it's actually quite easy for me to add this at runtime, without relying on the JIT compiler - IL is a lot easier to handle than x86 assembly. – Luaan Jul 14 '16 at 12:48
  • 3
    @Luaan You confuse what’s legal with what’s technically possible. C++ can only do this because it generates a different outside function for every template type (for details, see http://stackoverflow.com/a/13722515/1968): the function itself is tagged with a (static) type. In .NET, the outside function isn’t tagged with distinct types, neither static nor dynamic. To generate separate code for it based on different parameters, the optimiser would have to solve a problem that is, as far as I know, [intractable](https://en.wikipedia.org/wiki/Computational_complexity_theory#Intractability). – Konrad Rudolph Jul 14 '16 at 13:26
  • 2
    @Luaan And, just to clarify: with that I don’t mean that it is *absolutely* impossible to perform this optimisation (that’s obviously silly), just that it’s impossible to do this in reasonable time (= constant time complexity with regards to expression complexity) — which is what matters when assessing whether a given optimisation can be performed at runtime. – Konrad Rudolph Jul 14 '16 at 13:32
  • @KonradRudolph Oh, but it doesn't have to be the same method. That already happens now in fact, for example with .NET's generics. And since you have delegate identity, it's possible to make this as efficient as templates. Don't forget that the JIT compiler deals with IL, which is a very high-level "assembly" language - it has tons of information. Don't get me wrong - it's definitely a much easier problem with C++'s templates, and as I said, I don't expect such an optimization to ever be implemented for .NET, since there's so many more useful things they can implement instead. – Luaan Jul 14 '16 at 13:35
  • @Luaan Apologies, got side-tracked by my own example, where `Sort` was a non-generic method in my mind. You’re right, it has a properly distinct type in .NET if it’s a generic (not sure about Java, which doesn’t have reified generics after all … but maybe the runtime keeps track of this data anyway). Yes, you’re right. – Konrad Rudolph Jul 14 '16 at 13:38
  • @KonradRudolph Yeah, in Java, generics are little more than syntactic sugar. In .NET, they are a runtime fact as well - though I certainly wished for C++ templates in C# at times :) But I think that there's some examples on the JVM/JRE where a more powerful quotation system is used to basically make all code into data, quite similar to LISP - maybe Clojure? In that case, all the indirection can be removed at any time at runtime, which should allow such inlining to work perfectly, long before you have to care about complications like local data slots. – Luaan Jul 14 '16 at 13:50
21

C++ (or C for that matter) gives you fine-grained control over your data structures. If you want to bit-twiddle you have that option. Large managed Java or .NET apps (OWB, Visual Studio 2005) that use the internal data structures of the Java/.NET libraries carry the baggage with them. I've seen OWB designer sessions using over 400 MB of RAM and BIDS for cube or ETL design getting into the 100's of MB as well.

On a predictable workload (such as most benchmarks that repeat a process many times) a JIT can get you code that is optimised well enough that there is no practical difference.

IMO on large applications the difference is not so much the JIT as the data structures that the code itself is using. Where an application is memory-heavy you will get less efficient cache usage. Cache misses on modern CPUs are quite expensive. Where C or C++ really win is where you can optimise your usage of data structures to play nicely with the CPU cache.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ConcernedOfTunbridgeWells
  • 64,444
  • 15
  • 143
  • 197
  • Worked for Cadence Design systems 2010 2013, on a large code base dating back 1989 or so, linux desktop application. Had tremendous gains investigating and fixing for months issues related to cache and page defaults. And the fine granularity of control c++ gives you is the tool to achieve this. Also figured out we had a 2x slowdown when migrating to x64 systems, fixed later, same type of issue : usage of pointers and registry in the cpu cache. C++ was truely unbeatable, perhaps RUST is now too abe to do the same thing. – rsacchettini Aug 30 '22 at 02:38
19

For graphics the standard C# Graphics class is way slower than GDI accessed via C/C++. I know this has nothing to do with the language per se, more with the total .NET platform, but Graphics is what is offered to the developer as a GDI replacement, and its performance is so bad I wouldn't even dare to do graphics with it.

We have a simple benchmark we use to see how fast a graphics library is, and that is simply drawing random lines in a window. C++/GDI is still snappy with 10000 lines while C#/Graphics has difficulty doing 1000 in real-time.

QBziZ
  • 3,170
  • 23
  • 24
  • 5
    I was intrigued by your answer. Have you tested the same benchmark with unsafe code and lockbits, and drawing the random lines yourself? Now *that* would be an interesting thing to look at. – Pedery Jun 10 '12 at 23:02
  • 2
    @Pedery nope I haven't. just using the GDI and .NET.Graphics in the most basic of ways. what do you mean by "drawing the random lines yourself"? – QBziZ Jul 03 '12 at 08:20
  • 1
    Then you should perhaps consider to test this to get a more realistic metrics for how fast C# can be. Here's a nice overview of the technique: http://www.bobpowell.net/lockingbits.htm – Pedery Jul 10 '12 at 14:33
  • 6
    That is not what we want to do, putting separate pixels in a frame buffer ourselves. If you have to implement everything yourself what's the point of having an API/Platform to code against? For me this is a non-argument. We never needed to put separate pixels in a framebuffer in GDI for drawing lines, and we are not planning to do this in .NET neither. In my view, we did use a realistic metric, and .NET turned out to be slow. – QBziZ Jul 18 '12 at 12:07
  • It's realistic since sometimes you need to speed up your graphics and you can do this while still working within C#. I think it's interesting you have tested this so it could have been moreso interesting to see whether the C# lockbits method is as fast as its C++ counterparts. – Pedery Jul 23 '12 at 07:33
  • Disagree i wrote a camere filter that does do blob detection in 20ms – Peter Apr 12 '16 at 07:00
  • 1
    Well, I have just a slight idea what blob detection is, but just stating one timing does rather prove nothing at all. Have you written one in C++? In JavaScript? And compared those to the one in C#? And besides that, I don't think blob detection is using many graphics primitives. Correct me if wrong, but I guess it's statistical algorithms performing operations on pixels. – QBziZ Apr 17 '16 at 10:07
  • 1
    @JonHarrop Um? WPF doesn't use GDI, or the C# Graphics class. And very slow compared to what? On what kind of hardware? And how do you measure "slow"? And how does C# WPF compare to C++ WPF? I agree that e.g. MFC in C++ is a lot faster than *Winforms* in .NET, but WPF isn't really comparable. I can easily have tens of thousands of controls in WPF without virtualisation, and still be snappy - neither native MFC nor Winforms comes anywhere close. – Luaan Jul 14 '16 at 08:17
  • This could be due to so many other factors - there is a lot of software stack between the functions which cause graphical lines to be stored prior to being rendered as pixels and the function calls from C++ or C#. – FreelanceConsultant Mar 09 '22 at 10:25
14

The garbage collection is the main reason Java# CANNOT be used for real-time systems.

  1. When will the GC happen?

  2. How long will it take?

This is non-deterministic.

  • 5
    I'm not a huge Java fan but there's nothing that says Java can't use a real-time friendly GC. – Zan Lynx Apr 01 '10 at 06:47
  • 5
    There are plenty of real-time GC implementations if you care to look. (GC is an area that is *overflowing* with research papers) – Arafangion May 19 '10 at 00:37
  • FWIW, Richard Jones just published an updated version of his garbage collection book that covers, amongst other things, state-of-the-art real-time GC designs. – J D Sep 07 '11 at 08:01
  • Just talking like "*there's a RTGC*" doesn't answer the question about determinism. I believe many people are still in doubt on this due to lack of real clear explanation. – eonil May 09 '14 at 10:42
  • 11
    This is a nonsense argument, Windows (and Linux) are not Real Time OSes. Your C++ code could be swapped out for a number of 18 ms slots at any time too. – H H May 17 '14 at 08:47
  • 2
    @HenkHolterman True, but you could always write a boot-loader in assembly, tie that into a kernel bootstrap for your application and execute your C++ apps directly against the hardware (in RT btw). You can't do this in C# and any efforts that I have seen only mimic pre-compiled assembly in C# and use a ton of C code, which make it point-less to use C#. Reading all this is kinda funny, because C# is truly useless without the .NET framework. – zackery.fix Jan 12 '16 at 06:54
  • realtime computers are a oxymoron – Peter Apr 12 '16 at 06:59
  • @zackery.fix Haha, no. I'm not saying that C# would be my first choice for an embedded system with 32 kiB of memory, but that's only because there's little point in optimizing any part of C# or .NET for that kind of environment. But if you have at least half a meg of memory, you're good to go. And I've written an OS in C# before, and I can tell you that it's awesome. Without the .NET framework, of course. It's funny how you say that C# can only "mimic" pre-compiled assembly, while in C++, you get this natively... with assembly. So you can you write an OS in C++ or not? :) – Luaan Jul 14 '16 at 08:22
  • Huh? Game engines are "real-time systems", and all of them do garbage collection, even in C++, because immediate deallocation of ref-counted objects is too expensive and interrupts gameplay. Garbage collection in real-time systems is a feature, not a bug, because you can put all your deallocation work at a time where it doesn't matter. – Per Jul 02 '21 at 10:40
  • This answer is completely wrong and does not make sense. When GC happens is completely deterministic, unless you are arguing external factors drive GC. (In which case, there is still no difference between when GC happens in Java or C#, or when functions to deallocate memory in C++ are triggered.) Examples of external factors might be user input (keyboard/mouse) or network traffic. These are generally beyond your control as a software developer, and so you could argue modelling them statistically is an appropriate thing to do. (eg: Assuming they are "random" is not totally unreasonable.) – FreelanceConsultant Mar 09 '22 at 10:28
  • In other words, something happens in your program which triggers GC. In C++ you might say that if a vector is declared in a function then the GC (dealloc) happens when the vector goes out of scope. There is nothing "random" about that. The same thing is true in any GC language. When the GC happens is not random but triggered by some, completely deterministic, process. That said, I am about to second-guess myself here. I assume there are no languages where something like a random number is generated which triggers a timer after a random length of time, which causes GC to occur. – FreelanceConsultant Mar 09 '22 at 10:29
13

C/C++ can perform vastly better in programs where there are either large arrays or heavy looping/iteration over arrays (of any size). This is the reason that graphics are generally much faster in C/C++, because heavy array operations underlie almost all graphics operations. .NET is notoriously slow in array indexing operations due to all the safety checks, and this is especially true for multi-dimensional arrays (and, yes, rectangular C# arrays are even slower than jagged C# arrays).

The bonuses of C/C++ are most pronounced if you stick directly with pointers and avoid Boost, std::vector and other high-level containers, as well as inline every small function possible. Use old-school arrays whenever possible. Yes, you will need more lines of code to accomplish the same thing you did in Java or C# as you avoid high-level containers. If you need a dynamically sized array, you will just need to remember to pair your new T[] with a corresponding delete[] statement (or use std::unique_ptr)—the price for the extra speed is that you must code more carefully. But in exchange, you get to rid yourself of the overhead of managed memory / garbage collector, which can easily be 20% or more of the execution time of heavily object-oriented programs in both Java and .NET, as well as those massive managed memory array indexing costs. C++ apps can also benefit from some nifty compiler switches in certain specific cases.

I am an expert programmer in C, C++, Java, and C#. I recently had the rare occasion to implement the exact same algorithmic program in the latter 3 languages. The program had a lot of math and multi-dimensional array operations. I heavily optimized this in all 3 languages. The results were typical of what I normally see in less rigorous comparisons: Java was about 1.3x faster than C# (most JVMs are more optimized than the CLR), and the C++ raw pointer version came in about 2.1x faster than C#. Note that the C# program only used safe code—it is my opinion that you might as well code it in C++ before using the unsafe keyword.

Lest anyone think I have something against C#, I will close by saying that C# is probably my favorite language. It is the most logical, intuitive and rapid development language I've encountered so far. I do all my prototyping in C#. The C# language has many small, subtle advantages over Java (yes, I know Microsoft had the chance to fix many of Java's shortcomings by entering the game late and arguably copying Java). Toast to Java's Calendar class anyone? If Microsoft ever spends real effort to optimize the CLR and the .NET JITter, C# could seriously take over. I'm honestly surprised they haven't already—they did so many things right in the C# language, why not follow it up with heavy-hitting compiler optimizations? Maybe if we all beg.

Special Sauce
  • 5,338
  • 2
  • 27
  • 31
  • 3
    _"you will just need to remember to pair your `new T[]` with a corresponding `delete[]`"_ – No you don't. There's `std::unique_ptr` to do that for you. – Emil Laine Dec 29 '15 at 01:11
  • asuming you wrote something in graphics why write safe code in c#, have you considered using unsafe code and compare again ?. – Peter Apr 12 '16 at 06:53
11

As usual, it depends on the application. There are cases where C# is probably negligibly slower, and other cases where C++ is 5 or 10 times faster, especially in cases where operations can be easily SIMD'd.

Dark Shikari
  • 7,941
  • 4
  • 26
  • 38
  • Best case for VMs will be run-time compilation of generated code (e.g. to match a regular expression read in at run time) because statically compiled vanilla C++ programs can only use interpretation because they do not have a JIT compiler built in. – J D Sep 06 '11 at 20:13
  • 1
    Note from the future: .NET does have support for SIMD and friends since about 2014, though it's not widely used. – Luaan Jul 14 '16 at 08:29
11

We have had to determine if C# was comparable to C++ in performance and I wrote some test programs for that (using Visual Studio 2005 for both languages). It turned out that without garbage collection and only considering the language (not the framework) C# has basically the same performance as C++. Memory allocation is way faster in C# than in C++ and C# has a slight edge in determinism when data sizes are increased beyond cache line boundaries. However, all of this had eventually to be paid for and there is a huge cost in the form of non-deterministic performance hits for C# due to garbage collection.

ILoveFortran
  • 3,441
  • 1
  • 21
  • 19
  • 2
    In C++, you have the option of using different allocation methods, so depending on how memory was allocated (AOT?) in C#, it could be done the same way (but much faster) in C++. – zackery.fix Jan 12 '16 at 06:56
  • 5
    @zackery.fix .NET has an interesting edge in heap allocation, because it only has to move a pointer to allocate a new object. This is only feasible due to the compacting garbage collector. Of course you can do the same thing in C++, but C++ doesn't do that. It's funny how you use the same argument to say "C# could but doesn't, so it's garbage" and "C++ doesn't, but it could, so it's awesome" :) – Luaan Jul 14 '16 at 08:27
10

I know it isn't what you were asking, but C# is often quicker to write than C++, which is a big bonus in a commercial setting.

Kramii
  • 8,379
  • 4
  • 32
  • 38
8

> From what I've heard ...

Your difficulty seems to be in deciding whether what you have heard is credible, and that difficulty will just be repeated when you try to assess the replies on this site.

How are you going to decide if the things people say here are more or less credible than what you originally heard?

One way would be to ask for evidence.

When someone claims "there are some areas in which C# proves to be faster than C++" ask them why they say that, ask them to show you measurements, ask them to show you programs. Sometimes they will simply have made a mistake. Sometimes you'll find out that they are just expressing an opinion rather than sharing something that they can show to be true.

Often information and opinion will be mixed up in what people claim, and you'll have to try and sort out which is which. For example, from the replies in this forum:

  • "Take the benchmarks at http://shootout.alioth.debian.org/ with a great deal of scepticism, as these largely test arithmetic code, which is most likely not similar to your code at all."

    Ask yourself if you really understand what "these largely test arithmetic code" means, and then ask yourself if the author has actually shown you that his claim is true.

  • "That's a rather useless test, since it really depends on how well the individual programs have been optimized; I've managed to speed up some of them by 4-6 times or more, making it clear that the comparison between unoptimized programs is rather silly."

    Ask yourself whether the author has actually shown you that he's managed to "speed up some of them by 4-6 times or more" - it's an easy claim to make!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
7

For 'embarassingly parallel' problems, when using Intel TBB and OpenMP on C++ I have observed a roughly 10x performance increase compared to similar (pure math) problems done with C# and TPL. SIMD is one area where C# cannot compete, but I also got the impression that TPL has a sizeable overhead.

That said, I only use C++ for performance-critical tasks where I know I will be able to multithread and get results quickly. For everything else, C# (and occasionally F#) is just fine.

Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
6

.NET languages can be as fast as C++ code, or even faster, but C++ code will have a more constant throughput as the .NET runtime has to pause for GC, even if it's very clever about its pauses.

So if you have some code that has to consistently run fast without any pause, .NET will introduce latency at some point, even if you are very careful with the runtime GC.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Florian Doyon
  • 4,146
  • 1
  • 27
  • 37
  • 9
    -1: This is actually a myth. Firstly, the latency of idiomatic C++ is actually awful and often much worse than .NET because RAII causes avalanches of destructors when large data structures fall out of scope whereas modern GCs are incremental and .NET's is even concurrent. Secondly, you can actually completely remove GC pauses on .NET by not allocating. – J D Sep 06 '11 at 20:09
  • 2
    If you do this, you then have to forego using the BCL as most of the methods create transient objects. – Florian Doyon Sep 07 '11 at 08:52
  • 5
    This is quite true, it wasn't until .net 4 that the GC was made incremental. We have a large C# app that pauses for seconds at a time for GC. For performance critical apps this is a killer. – Justin Nov 10 '11 at 22:32
  • 5
    There's a reason why programs that tend to push the hardware tend to use C++. You have more fine tuned control when you need it. Performance is only key when you're pushing the system, otherwise use C# or Java to save you time. – VoronoiPotato Oct 30 '12 at 13:51
  • 1
    In terms of C# game dev: You can properly code a game to never use the GC until a level transition. All math is done via structs, this means you won't need to init objects or toss a continuous load onto the GC. In fact, the biggest optimization you can do for C# game dev is to try not to use the GC at all. C# also allows you to force GC at any time, which can be done at level transitions. Basically, the GC is only a problem if you let it be. – Krythic Jan 01 '16 at 03:08
  • 5
    if you can't manage the cache behavior, you can't beat optimized c++ code. A cache miss from L1 to main memory could slow your operation 100 times. – DAG Mar 17 '16 at 05:20
  • @JonHarrop Just move your destructors to a seperate thread. Gone are the latencies. – Clearer Nov 30 '17 at 10:16
  • @DAG: True but you can manage the cache behaviour in all of these languages, of course. – J D Dec 16 '17 at 02:30
  • 1
    @Clearer: You're Greenspunning a GC. – J D Dec 16 '17 at 02:32
  • @JonHarrop Not at all. A garbage collector identifies which part of memory is safe to reclaim and then reclaims it -- either in the same thread as the rest of the program or in some other thread. I'm moving the clean up code from one thread to another. I still manage the clean up and do not rely on magic to deal with it. – Clearer Dec 18 '17 at 12:37
  • 1
    @Clearer: So you're saying that you're not Greenspunning a GC because you're not using "magic"? – J D Dec 20 '17 at 20:21
  • @JonHarrop Any proper GC will traverse all allocated memory to figure out which bits are no longer reachable and will then reclaim those bits. That's not what's going on if you move the destruction of objects to a different thread; you're manually declaring that those bits are safe to reclaim with all the benefits and problems that you usually have, and none (as in not a single one) of the benefits of a GC. A plain GC *will* introduce latencies at semi-random locations in your program; usually much worse than RAII will and a lot less predictably. – Clearer Jan 02 '18 at 13:50
  • @Clearer: "Any proper GC will traverse all allocated memory to figure out which bits are no longer reachable and will then reclaim those bits". Reference counting is an obvious counter example. "A plain GC will introduce latencies at semi-random locations in your program; usually much worse than RAII". I have only ever seen latencies decrease when RAII is replaced with a GC so I'd love to see that hypothesis tested properly. – J D Jan 02 '18 at 22:11
  • @JonHarrop Reference counting does exactly what I described or it's potentially broken. I have never experienced any significant delays caused by destruction of objects using RAII; if you're seeing decreased latencies with a GC, I can only imagine one of the following scenarios: (1) you're something wrong (i.e. allocating 1 object 1000 times, rather than 1000 objects at a time), (2) the GC is not doing what it's telling you it's doing (not cleaning everything up) or (3) it's moving destruction to a separate thread. I've seen GCs move destruction to a new threads before. – Clearer Jan 02 '18 at 22:44
  • @Clearer: "Reference counting does exactly what I described or it's potentially broken". Sorry but that's just not how RC works. I suggest you read http://gchandbook.org/ – J D Jan 03 '18 at 14:32
  • 1
    @Clearer: "I can only imagine one of the following scenarios". When collections of collections fall out of scope the parent destructors recursively call the child destructors until the entire DAG has been destructed. That is an arbitrarily-long pause. Incremental GCs don't do that. – J D Jan 03 '18 at 14:38
  • C++ : Long optimization phase, done one time before running. – rxantos Sep 10 '19 at 03:17
4

It's an extremely vague question without real definitive answers.

For example; I'd rather play 3D-games that are created in C++ than in C#, because the performance is certainly a lot better. (And I know XNA, etc., but it comes no way near the real thing).

On the other hand, as previously mentioned; you should develop in a language that lets you do what you want quickly, and then if necessary optimize.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David The Man
  • 259
  • 1
  • 4
  • 11
  • 4
    Could you name a few examples? Games written in C# what you've found slow – Karl Sep 26 '08 at 14:10
  • 1
    Even the example applications that came with the installation felt slow. – David The Man Sep 30 '08 at 05:35
  • 10
    The garbage collector is a huge liability in making games with C#, as it can kick in any time, causing major pauses. Explicit memory management ends up being easier for game development. – postfuturist Oct 30 '08 at 22:44
  • 4
    Most modern games are GPU-limited. For such games it does not matter if the logic (executed on CPU) is 10% slower, they are still limited by GPU, not CPU. Garbage collector is a real problem, causing random short freezes if the memory allocations are not tuned well. – Michael Entin Nov 26 '08 at 18:05
  • 2
    @postfuturist: That is not true on PC; the garbage collector does such a good job of getting in and out I've never experienced any problems with it. However, on XBox 360 and Zune/Windows-7-Phone, the garbage collector is not *nearly* as smart as on PC; I've never written for either, but [people who have](http://gamedev.stackexchange.com/questions/7/what-are-typical-pitfalls-when-writing-games-with-a-managed-language-like-c/210#210) tell me the garbage collector is a *huge* problem. – BlueRaja - Danny Pflughoeft Jun 01 '11 at 22:06
  • @BlueRaja [There’s a great article](http://sealedabstract.com/rants/why-mobile-web-apps-are-slow/) which explains how and when GCs are slow. Given high enough memory pressure, they can be slow even on a PC. I don’t know the *exact* requirements for AAA game titles but it’s not hard to imagine that they routine go beyond the boundaries within which a modern GC performs well. – Konrad Rudolph Nov 05 '13 at 23:20
  • @KonradRudolph Although games often use a lot of memory, most of it can remain untouched. Especially with loading screens a lot of data is retrieved from the HDD, put in RAM, transferred to video card, and some of it can be reclaimed. By forcing a full collection of the GC after that loading phase, a lot of pressure can be released during run time. General game's performance problems come from requiring lots of processing power in small time frames while having spare in others. The GC is pretty good at determining when you have spare, and will try to collect in those frames. – Aidiakapi May 09 '14 at 12:11
  • @Aidiakapi Not to contradict you in principle, but when (in the game loop during normal game time) is there so much less pressure that a large collection could run without impacting the experience? Also, even when memory remains untouched it still needs to be available. Maybe pervasive use of weak references (to implement a cache, and reload from secondary memory upon collection) could help, but GCs only work well when there is vastly more memory available than in use. And finally, theory’s one thing: in practice, which modern GC reliably does what you’ve described? – Konrad Rudolph May 09 '14 at 13:26
  • @postfuturist If you program your game correctly you won't use the GC at all. Most data types and calculations are done via structs, and objects typically persist during the entire lifetime of a level. When the level is over, simply force GC before loading the next level. (Speaking of C# in this regard) – Krythic Jan 01 '16 at 03:02
  • @Karl I also have consistently seen that games written in C# are slow. Believe it or not, I actually look to see if a game was made in C# because i would try to avoid it due to performance reasons it's so bad. The Forest, 7 Days To Die, Broforce, Escape Plan, And on and on. I REALLY like Broforce though, so i put up with it. C# games are consistently embarrassingly slow in my experience. It is very possible that the developers did a poor job on optimization though. C# is one of my favorite languages however, but it is rather slow for real-time applications – Richmar1 Mar 05 '19 at 02:55
4

In theory, for long running server-type application, a JIT-compiled language can become much faster than a natively compiled counterpart. Since the JIT compiled language is generally first compiled to a fairly low-level intermediate language, you can do a lot of the high-level optimizations right at compile time anyway. The big advantage comes in that the JIT can continue to recompile sections of code on the fly as it gets more and more data on how the application is being used. It can arrange the most common code-paths to allow branch prediction to succeed as often as possible. It can re-arrange separate code blocks that are often called together to keep them both in the cache. It can spend more effort optimizing inner loops.

I doubt that this is done by .NET or any of the JREs, but it was being researched back when I was in university, so it's not unreasonable to think that these sort of things may find their way into the real world at some point soon.

Eclipse
  • 44,851
  • 20
  • 112
  • 171
4

Applications that require intensive memory access eg. image manipulation are usually better off written in unmanaged environment (C++) than managed (C#). Optimized inner loops with pointer arithmetics are much easier to have control of in C++. In C# you might need to resort to unsafe code to even get near the same performance.

Kalle
  • 141
  • 2
  • 2
4

I've tested vector in C++ and C# equivalent - List and simple 2d arrays.

I'm using Visual C#/C++ 2010 Express editions. Both projects are simple console applications, I've tested them in standard (no custom settings) release and debug mode. C# lists run faster on my pc, array initialization is also faster in C#, math operations are slower.

I'm using Intel Core2Duo P8600@2.4GHz, C# - .NET 4.0.

I know that vector implementation is different than C# list, but I just wanted to test collections that I would use to store my objects (and being able to use index accessor).

Of course you need to clear memory (let's say for every use of new), but I wanted to keep the code simple.

C++ vector test:

static void TestVector()
{
    clock_t start,finish;
    start=clock();
    vector<vector<double>> myList=vector<vector<double>>();
    int i=0;
    for( i=0; i<500; i++)
    {
        myList.push_back(vector<double>());
        for(int j=0;j<50000;j++)
            myList[i].push_back(j+i);
    }
    finish=clock();
    cout<<(finish-start)<<endl;
    cout<<(double(finish - start)/CLOCKS_PER_SEC);
}

C# list test:

private static void TestVector()
{

    DateTime t1 = System.DateTime.Now;
    List<List<double>> myList = new List<List<double>>();
    int i = 0;
    for (i = 0; i < 500; i++)
    {
        myList.Add(new List<double>());
        for (int j = 0; j < 50000; j++)
            myList[i].Add(j *i);
    }
    DateTime t2 = System.DateTime.Now;
    Console.WriteLine(t2 - t1);
}

C++ - array:

static void TestArray()
{
    cout << "Normal array test:" << endl;
    const int rows = 5000;
    const int columns = 9000;
    clock_t start, finish;

    start = clock();
    double** arr = new double*[rows];
    for (int i = 0; i < rows; i++)
        arr[i] = new double[columns];
    finish = clock();

    cout << (finish - start) << endl;

    start = clock();
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < columns; j++)
            arr[i][j] = i * j;
    finish = clock();

    cout << (finish - start) << endl;
}

C# - array:

private static void TestArray()
{
    const int rows = 5000;
    const int columns = 9000;
    DateTime t1 = System.DateTime.Now;
    double[][] arr = new double[rows][];
    for (int i = 0; i < rows; i++)
        arr[i] = new double[columns];
    DateTime t2 = System.DateTime.Now;

    Console.WriteLine(t2 - t1);

    t1 = System.DateTime.Now;
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < columns; j++)
            arr[i][j] = i * j;
    t2 = System.DateTime.Now;

    Console.WriteLine(t2 - t1);

}

Time: (Release/Debug)

C++

  • 600 / 606 ms array init,
  • 200 / 270 ms array fill,
  • 1sec /13sec vector init & fill.

(Yes, 13 seconds, I always have problems with lists/vectors in debug mode.)

C#:

  • 20 / 20 ms array init,
  • 403 / 440 ms array fill,
  • 710 / 742 ms list init & fill.
Sam
  • 7,252
  • 16
  • 46
  • 65
Wiory
  • 201
  • 3
  • 5
  • 1
    I'd love to see index accessor in std::list. Anyway, it takes 37 secs with list, release mode. Release without debugging: 3s list, 0,3 s vector. Probably dereferencing issue or sth. Sample: http://www.nopaste.pl/12fb – Wiory Jun 23 '11 at 15:48
  • 1
    @doug65536 yes I realise that now, having done C#. Perhaps I'll just remove all that from history... – hiddensunset4 Jan 30 '13 at 07:59
  • 1
    @Daniel ok, I'll remove my comment too. – doug65536 May 23 '13 at 16:10
  • 2
    For more precise measurements you shouldn't be using `System.DateTime.Now`, but rather, the [Stopwatch](http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx) class. – Sam Aug 24 '14 at 23:17
  • 4
    Part of the reason you are getting such slow fill times for the vector in C++ is you are using push_back. This has been shown on numerous posts to be slower than using the at method or operator []. In order to use either of those methods you need to use the resize or reserve method. Additionally, the reason your initialization is taking so long for c++ vector case is that you are forcing a copy or assignment operator [not sure which in this case) to initialize your c++ vector. For the array in c++ there is an algorithm that uses 2 new calls rather than 5001 and is faster iterating as well. – Zachary Kraus Nov 15 '14 at 09:55
  • 5
    I think you didn't do c++ in a appropriate way. Just a glance and found so many issues. E.g. vector> myList=vector>() – DAG Mar 17 '16 at 02:44
  • 1
    And have you noticed how many times you constructed such a 2d vector? And do you mind do a reserve() to accelerate push_back()? I think your benchmark does not make any sense. And the way you measures clocks are far from accurate as well. Just my 2 cents – DAG Mar 17 '16 at 03:14
  • 1
    vector> myList=vector>() I see C# devs who write C++ do this all the time, this is SLOW and USELESS, don't make pointless copies of stuff – paulm Jul 11 '16 at 07:31
  • This is plainly wrong. I get totally different results, where C# is slower by a significant margin in both tests: 177% for array, and 212% for vector/list. – Johan Boulé Jul 17 '16 at 04:24
  • 2
    Wow. Not sure what conclusions one can draw from comparing Lists versus resizable arrays, but if you're going to use vectors like this, you'll want to learn about reserve(), my friend, reserve(). – U007D Sep 07 '16 at 20:56
3

If I'm not mistaken, C# templates are determined at runtime. This must be slower than compile time templates of C++.

And when you take in all the other compile-time optimizations mentioned by so many others, as well as the lack of safety that does, indeed, mean more speed...

I'd say C++ is the obvious choice in terms of raw speed and minimum memory consumption. But this also translates into more time developing the code and ensuring you aren't leaking memory or causing any null pointer exceptions.

Verdict:

  • C#: Faster development, slower run

  • C++: Slow development, faster run.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HumbleWebDev
  • 555
  • 4
  • 20
3

There are some major differences between C# and C++ on the performance aspect:

  • C# is GC / heap based. The allocation and GC itself is overhead as the non locality of the memory access
  • C++ optimizer's have become very good over the years. JIT compilers cannot achieve the same level since they have only limited compilation time and don't see the global scope

Besides that programmer competence plays also a role. I have seen bad C++ code where classes where passed by value as argument all over the place. You can actually make the performance worse in C++ if you don't know what you are doing.

gast128
  • 1,223
  • 12
  • 22
3

I found this April 2020 read: https://www.quora.com/Why-is-C-so-slow-compared-to-Python by a real-world programmer with 15+ years of Software Development experience.

It states that C# is slower usually because it is compiled to Common Intermediate Language (CIL) instead of machine code like C++. The CIL is then put through Common Language Runtime (CLR) which outputs machine code. However, if you keep executing C# it will take the output of the machine code and cache it so the machine code is saved for the next execution. All in all, C# can be faster if you execute multiple times since it is in machine code after multiple executions.

There is also comments that a good C++ programmer can do optimizations that can be time consuming that will in end be optimized.

Ryan Harlich
  • 157
  • 1
  • 7
  • C# isnt slower than python. I made a simple test program that creates a 1024*1024*8 sized byte array and then fills it with index % 255 and it took 1.4 seconds in python and 45ms in C# (first execution, not after running it multiple times) - also the question was how much faster C++ is, not python – GoldenretriverYT Apr 13 '22 at 09:04
  • Quora answers consist of many opinions but very little real knowledge. Python performance is not even close to that of C#, C++, or any compiled language. Furthermore, whatever author described the "reason" for the perceived performance difference does not understand how intermediate compilation works, nor that Python does something somewhat similar. – Charles Burns Nov 15 '22 at 17:33
2

I suppose there are applications written in C# running fast, as well as there are more C++ written apps running fast (well C++ just older... and take UNIX too...)
- the question indeed is - what is that thing, users and developers are complaining about ...
Well, IMHO, in case of C# we have very comfort UI, very nice hierarchy of libraries, and whole interface system of CLI. In case of C++ we have templates, ATL, COM, MFC and whole shebang of alreadyc written and running code like OpenGL, DirectX and so on... Developers complains of indeterminably risen GC calls in case of C# (means program runs fast, and in one second - bang! it's stuck).
To write code in C# very simple and fast (not to forget that also increase chance of errors. In case of C++, developers complains of memory leaks, - means crushes, calls between DLLs, as well as of "DLL hell" - problem with support and replacement libraries by newer ones...
I think more skill you'll have in the programming language, the more quality (and speed) will characterize your software.

bgee
  • 989
  • 2
  • 13
  • 19
2

Well, it depends. If the byte-code is translated into machine-code (and not just JIT) (I mean if you execute the program) and if your program uses many allocations/deallocations it could be faster because the GC algorithm just need one pass (theoretically) through the whole memory once, but normal malloc/realloc/free C/C++ calls causes an overhead on every call (call-overhead, data-structure overhead, cache misses ;) ).

So it is theoretically possible (also for other GC languages).

I don't really see the extreme disadvantage of not to be able to use metaprogramming with C# for the most applications, because the most programmers don't use it anyway.

Another big advantage is that the SQL, like the LINQ "extension", provides opportunities for the compiler to optimize calls to databases (in other words, the compiler could compile the whole LINQ to one "blob" binary where the called functions are inlined or for your use optimized, but I'm speculating here).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Quonux
  • 2,975
  • 1
  • 24
  • 32
  • 2
    Any proper C++ developer will not run into the problems you describe. Only bad C programmers who decided to slap classes on their programs and call it C++ have those problems. – Clearer Jan 03 '18 at 08:42
  • 1
    for the love of gods, this is 8 years old, OMFGz – Quonux Jan 08 '18 at 20:28
  • feel free to give a better more up to date answer – Quonux Jan 08 '18 at 20:29
  • We should also consider the compile-time metaprogramming capability of c++ which enables developers to execute pieces of code at compile time. Note modern c++ is more like C with templates. It's more towards functional programming compared to OOPS. The introduction of constexp had made metaprogramming easier to read and write. – user982042 May 06 '21 at 18:43
2

I would put it this way: programmers who write faster code, are the ones who are the more informed of what makes current machines go fast, and incidentally they are also the ones who use an appropriate tool that allows for precise low-level and deterministic optimisation techniques. For these reasons, these people are the ones who use C/C++ rather than C#. I would go as far as stating this as a fact.

Johan Boulé
  • 1,936
  • 15
  • 19
  • Notch coded minecraft to be pretty fast considering the amount of data he's manipulating. Also, he coded it mostly single-handedly in a comparatively short amount of time, something that would have been virtually impossible in C++. I do agree with the optimization techniques though--if you have the extra 10x dev time to spend so your code runs twice as fast, it's probably worth it. – Bill K Mar 01 '18 at 23:21
2

One area that I was instrumenting code in C++ vs C# was in creating a database connection to SQL Server and returning a resultset. I compared C++ (Thin layer over ODBC) vs C# (ADO.NET SqlClient) and found that C++ was about 50% faster than the C# code. ADO.NET is supposed to be a low-level interface for dealing with the database. Where you see perhaps a bigger difference is in memory consumption rather than raw speed.

Another thing that makes C++ code faster is that you can tune the compiler options at a granular level, optimizing things in a way you can't in C#.

Charles Owen
  • 2,403
  • 1
  • 14
  • 25
1

> After all, the answers have to be somewhere, haven't they? :)

Umm, no.

As several replies noted, the question is under-specified in ways that invite questions in response, not answers. To take just one way:

And then which programs? Which machine? Which OS? Which data set?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I fully agree. I wonder why people expect a precise answer (63.5%), when they ask a general question. I don't think there is no general answer to this kind of question. – call me Steve Nov 24 '08 at 23:54
  • @callmesteve: I know what you mean, but your last sentence should sound like nails over a chalk board to any programmer. – Wouter van Nifterick Nov 06 '10 at 08:52
  • 1
    This doesn't appear to answer the question, and reads more as a comment or rant. – Tas Jun 17 '16 at 00:20
1

It really depends on what you're trying to accomplish in your code. I've heard that it's just stuff of urban legend that there is any performance difference between VB.NET, C# and managed C++. However, I've found, at least in string comparisons, that managed C++ beats the pants off of C#, which in turn beats the pants off of VB.NET.

I've by no means done any exhaustive comparisons in algorithmic complexity between the languages. I'm also just using the default settings in each of the languages. In VB.NET I'm using settings to require declaration of variables, etc. Here is the code I'm using for managed C++: (As you can see, this code is quite simple). I'm running the same in the other languages in Visual Studio 2013 with .NET 4.6.2.

#include "stdafx.h"

using namespace System;
using namespace System::Diagnostics;

bool EqualMe(String^ first, String^ second)
{
    return first->Equals(second);
}
int main(array<String ^> ^args)
{
    Stopwatch^ sw = gcnew Stopwatch();
    sw->Start();
    for (int i = 0; i < 100000; i++)
    {
        EqualMe(L"one", L"two");
    }
    sw->Stop();
    Console::WriteLine(sw->ElapsedTicks);
    return 0;
}
Charles Owen
  • 2,403
  • 1
  • 14
  • 25
-14

Inspired by this, I did a quick test with 60 percent of common instruction needed in most of the programs.

Here’s the C# code:

for (int i=0; i<1000; i++)
{
    StreamReader str = new StreamReader("file.csv");
    StreamWriter stw = new StreamWriter("examp.csv");
    string strL = "";
    while((strL = str.ReadLine()) != null)
    {
        ArrayList al = new ArrayList();
        string[] strline = strL.Split(',');
        al.AddRange(strline);
        foreach(string str1 in strline)
        {
            stw.Write(str1 + ",");
        }
        stw.Write("\n");
    }
    str.Close();
    stw.Close();
}

String array and arraylist are used purposely to include those instructions.

Here's the c++ code:

for (int i = 0; i<1000; i++)
{
    std::fstream file("file.csv", ios::in);
    if (!file.is_open())
    {
        std::cout << "File not found!\n";
        return 1;
    }

    ofstream myfile;
    myfile.open ("example.txt");
    std::string csvLine;

    while (std::getline(file, csvLine))
    {
        std::istringstream csvStream(csvLine);
        std::vector csvColumn;
        std::string csvElement;

        while( std::getline(csvStream, csvElement, ‘,’) )
        {
            csvColumn.push_back(csvElement);
        }

        for (std::vector::iterator j = csvColumn.begin(); j != csvColumn.end(); ++j)
        {
            myfile << *j << ", ";
        }

        csvColumn.clear();
        csvElement.clear();
        csvLine.clear();
        myfile << "\n";
    }
    myfile.close();
    file.close();
}

The input file size I used was 40 KB.

And here's the result -

  • C++ code ran in 9 seconds.
  • C# code: 4 seconds!!!

Oh, but this was on Linux... With C# running on Mono... And C++ with g++.

OK, this is what I got on Windows – Visual Studio 2003:

  • C# code ran in 9 seconds.
  • C++ code – horrible 370 seconds!!!
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rks
  • 1
  • 1
  • 8
    You're using different data structures and library code there, although "370 seconds" does indicate something horrible - you aren't running it in the debugger by any chance are you? I suspect that the performance of the CSV library you are using is more interesting than the performance of the language you are using. I would question the use of a vector in that context, and what optimisations you used. Additionally, it is widely known that iostreams (in particular, the "myfile << *j << ", ";") is much slower than other methods of writing to the file, for at least some common implementations. – Arafangion May 18 '10 at 03:14
  • 6
    Finally, you're doing more work in the C++ version. (Why are you clearing the csvColumn, csvElement and csvLines?) – Arafangion May 18 '10 at 03:14
  • 3
    Every iteration of the while loop is going to destruct and reconstruct a std::istream and a std::vector and a std::string. The while body goes out of scope every iteration, all those variables inside the while scope are going to destruct and construct on every iteration. – doug65536 Jan 29 '13 at 18:45
  • 2
    from the looks of reading your c++ code you are trying to copy from one file to another file. Instead of using the complex interactions between file streams, strings, vectors and string streams, you could have just copied the input file stream to the output file stream. This would have saved a lot of time and memory. – Zachary Kraus Nov 15 '14 at 10:13
  • 3
    to do speed tests, test things in memmory dont get to disk IO, unsless your testing on the latest SSD's and its dedicated to your performance app. As computers constantly write to disk, even if you dont touch the keyboard. – Peter Apr 12 '16 at 06:57
  • I'd say the fairest way to compare the languages would be to use their own native tools, and perform the operations in almost exactly the same way. Maybe using each language's default string.split function and reading in line by line the default way would have been better. – HumbleWebDev Jul 21 '17 at 20:51
  • Did you even compile with optimizations enabled? 9 sec vs. 370 sec sounds unlikely, unless MSVC missed optimizing away some very expensive stuff that g++ found. – Peter Cordes Apr 07 '18 at 07:14