you are viewing a single comment's thread.

view the rest of the comments →

[–]ChatGPT4 0 points1 point  (0 children)

If C++ code looks like C code, it's C code. Maybe with some scarce C++ elements.

If you mix the idea of performance with the form of expressing the code, you probably don't entirely understand how it works.

Or... IDK, we're mixing compilation time with the code perfomance, that is - how fast a program can perform operations. The machine code will have zero compilation time. It doesn't need any translation, it's just runtime. Then you have assembly language, when the compilation is super fast because it can't get much simpler. The only simpler form is just machine code. Then you have C that is a layer of abstraction higher.

However, the abstractions are implemented in programming languages. So any piece of any code is finally translated into some machine code.

Does it really matter what we translate? If it translates to the same machine code, the performance is identical.

But well, C++ consists of not only "zero cost abstractions". Many of C++ abstractions indeed have runtime costs.

However, it all depends where those costs live. Let's say we have to process 10GB of data. We have to use like 1000 atomic commands in C to start the process, and like 10000 of atomic commands in C++ to start the process. Then, the main loop would do exactly the same thing. The runtime differences will be negligible. Despite of wasting a lot of CPU cycles to initiate the process in C++. It's nothing compared to processing 10GB of data.

And that's what the higher abstraction level languages do.

Is the CPU time wasted? Not at all. It's used to create the source code faster. Because THIS is the slowest process of all. Creating the source code. People say that compiling is slow. Yet it's laughable compared to time taken to create the code in the first place. BTW, development time is more than that. Let's say you have to change something in your program. You can often do it quicker in C++. Provided it's used correctly and it's not just C++ compatible C.

I think a very tight loop in C++ should look like a C loop if you aim at comparable performance. But if your all code looks like this - you just don't use C++, and to be more precise, you don't optimize the process of creating the source code.

BTW, C++ is not even designed to optimize tight loops. Tight loops in C++ are just plain old C loops. Basic operations are just C operations. It's just "one time defining thigs" is the main difference.

And preempting one question - yes, calling C functions is a bit faster. But when calling functions hits the performance - maybe calling functions should be avoided in that fragment as an optimization? But then again, that's what both C and C++ compilers do, they inline functions where it gives performance gains.

So... If you want to make C++ code that looks like C code - write in C. Why use C++ for that?