C++ slowness

This is a discussion on C++ slowness within the C++ Programming forums, part of the General Programming Boards category; Is the innate slowness in C++ due to the extra layers of abstraction that the computer must pick through in ...

Innate slowness? I'd say that's due to poor use of the language, not the language itself. Unless you are using assembly, you're probably not going to get much faster, and even the it's not going to be any remarkable speed increase. Of course, it's really tough to say whether the "language" is slow or fast, because that's all dependant on the compiler and your system, etc. not the language itself.

I've heard that it is possible for perfectly written C++ code to have overhead when compared to perfectly written C code due to the nature of the language itself (I'm not examining sytem or compiler, although I know those are huge huge huge factors). I just wanted to know what aspect of the language could cause any possible slowness, and it stills seems that if the overhead exists its because of what I said in my first post.

Poly when you have the plane equation, it is basically the dot product between the normal to the plane and the point in space, and then the distance is added(shortest distance from plane to origin).

I was just wondering, why exacdtly is the distance added?

What does it mean if you don't add the distance? It seems to me if you don't add the distance you might get points passing the test when they in fact should be failing (and vice versa) but I'm not entirely sure how it works, and my book doesn't touch upon that really.

When trying to guestimate speed in assembly can you just count the number of instructions?

Sort-of. Each instruction takes a different number of clock cycles.
When microprocessors were simple, you could look-up the number of cycles required for each particular instruction.

But, things have gotten more complicated.... with parallel processing, predictive branching, on-board cache. Now, they talk in terms of instructions-per-cycle. This spec is an average when performing a standard benchmark test.

The speed issue/question really depends on what you need the speed for, because 90% of the time, processing speed is not a problem. If you need high-speed graphics, then you need to know how to get the most from the graphics drivers and libraries. Maybe you're doing massive scientific "number crunching"... then you need fast floating point calculations. Maybe you're doing transaction processint... then you need fast I/O and disk perfromance.

in addition to what DougDbug said above, most assembly languages also have Pseudo-Instructions. that is, a one line instruction that is valid syntax, but actually gets executed as a series of more simple instructions. thus, just counting the number of instructions used is not very accurate.

Depending on the processor, the reference books will show you how many cycles for that certain instruction. In some cases you could use a different instruction to do the same thing, but one has less clock cycles.

Originally posted by SAMSAM writing a C++ program requires a truly sbstantial amount of
coding compare to for instance, a C version.

What's stopping one from doing it the same way in C++ as done in C if you wanted? If you're trying to compare purely the object-oriented aspects of C++ i think you may be missing the point of the question being asked, , even still, if you use the language properly, for a large project, object-oriented C++ will most-likely be less code than a C equivalent, depending on the type of the project. Again, the question doesn't seem to address just oop C++, it sounds as though it's addressing the runtime aspects of a program written in C when compiled on a C compiler and when compiled on a C++ compiler. If not then the languages are impossible to compare because you can't compare features in C++ to ones which simply don't even exist in C.

Originally posted by SAMSAM matrix operations in C++ are much slower than C .and
there are aspects of C++ design that making it slower to
access its features.

Matrix operations? What "operations" are you talking about? Care to elaborate, or are you just happy pulling things out of yer arse Pointer arithmetic hasn't changed from C to C++. There isn't any reason that accesing an element of a multidimensional array would be at all slower than in C. There are, of course, very small places where C++ CAN be slower in extremely small respects to a C "equivalent," such as, in some cases, dynamic memory allocation, default constructors called during allocation, exception handling etc., but there is no reason that one can use placement new to get around the default constructor situation and if worst came to worst you can always use C-style dynamic memory allocation in C++. There's nothing stopping you from doing so. Post some real places where C++ can be slower than C, or elaborate on this one, and then I'll give you some credibility. There are places where C++ can [potentially] be slower than a C equivalent, all of which are so trivial and there for good reason that you're not going to find people complaining about them.

Originally posted by SAMSAM STL vectore vs ordinary vector.

First off -- the STL is not the C++ language, and even if it was, you don't have to use vectors as a C++ programmer (I don't). Don't confuse libraries with the language itself. There is no such thing as a vector<> in the C++ language, only in the STL.

Originally posted by SAMSAM you can take a coffee break at compilation time for templates.

You think that compiling templates takes a long time? To do the equivalent in C, you would have to completely rewrite each function that was templated for each datatype or value, which would take you much more time for you to write than a templated function would take to be written and compiled (keeping in mind that templated definitions only have to be compiled when you use a form of the templated function, unlike if you were to rewrite the function each time in C), plus, in C++ with templates, you are allowing for future datatypes, which just isn't possible in C without having to manually rewrite the function again and again with slight variances. Not only does it take much longer, but it makes it much more likely to being in bugs and will make your code much longer than it should be.

But what it really comes down to is that it all depends on your compiler and system. A "language" can't be slow, it's is just syntax and keywords meaning nothing in terms of speed. It's impossible to compare languages like that because they, themselves, don't have execution time. I understand the point of the question, but it really doesn't have a valid answer.

First off -- the STL is not the C++ language, and even if it was, you don't have to use vectors as a C++ programmer (I don't). Don't confuse libraries with the language itself. There is no such thing as a vector<> in the C++ language, only in the STL.

Dear oh dear poly i gave you more credit than that. Read your c++ standard specifically section 23 ( containers) and you will see just how wrong that statement is. The stl no longer exists as a library but it has been fully incorporated into the language. Are you saying there is no std::string type too cos thats an stl container.In fact about the only thing the standard doesnt mandate for vector is that it should be a dyamic array. What it does do tho is to specify the properties of a vector as properties of an dynamic array.

Originally posted by Stoned_Coder Dear oh dear poly i gave you more credit than that. Read your c++ standard specifically section 23 ( containers) and you will see just how wrong that statement is. The stl no longer exists as a library but it has been fully incorporated into the language. Are you saying there is no std::string type too cos thats an stl container.In fact about the only thing the standard doesnt mandate for vector is that it should be a dyamic array. What it does do tho is to specify the properties of a vector as properties of an dynamic array.