Abstractions and Performance

Need help with C, C++, perl, python, etc?

Abstractions and Performance

Postby neuraleskimo » 2020-04-06 18:02

There have been a number of discussions here and elsewhere on the Internet about abstractions, performance, and C vs C++. Today, I was considering a few implementations and found a really good example.
Code: Select all
#include <array>

int f1(int num) {
    static std::array<long, 10> mult {
        0, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1};
    return mult[num];
}

int f2(int num)
{
    static long mult[] = {
        0, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1};
 
    return mult[num];
}


Compiler output (GCC 8.3 with -O3):
Code: Select all
f1(int):
        movsx   rdi, edi
        mov     rax, QWORD PTR f1(int)::mult[0+rdi*8]
        ret
f2(int):
        movsx   rdi, edi
        mov     rax, QWORD PTR f2(int)::mult[0+rdi*8]
        ret
f2(int)::mult:
        .quad   0
        .quad   100000000
        .quad   10000000
        .quad   1000000
        .quad   100000
        .quad   10000
        .quad   1000
        .quad   100
        .quad   10
        .quad   1
f1(int)::mult:
        .quad   0
        .quad   100000000
        .quad   10000000
        .quad   1000000
        .quad   100000
        .quad   10000
        .quad   1000
        .quad   100
        .quad   10
        .quad   1


Both implementations are identical! One of these uses the new c++ array class and the other looks (other than the static keyword) like it could have been written 30 years ago. Just because an abstraction provides a safer interface, it does not mean slower or bigger.

When someone says, why can't I use macro, pointers, hand-rolled data-structures, etc. You can (I'll come to that in a moment), but really only as a last resort or with really good reason. Frankly, the world is full of buggy code. Please don't contribute to it. Be safe first, measure second, and use the sharp knives when necessary.

Now, back to using the sharp knives... Yes, I am guilty playing with knives, but I try to only bring them out when necessary. If you look at some of my code on GitHub, you will see that I have used macros, for example, but only because there is no alternative. See https://github.com/tonywalker1/stuff/blob/master/include/stuff/core/exception.h.

If you are curious about the code above, I am using a library function that is slower than I need. I rolled my own to shave a factor of 10 of the run-time. One of the tricks in my toolbox is to use lookup tables for special circumstances. Lookups are always constant time and often faster than other techniques (assuming small tables, big enough caches, etc.). That is what the code above prototypes. By the way, CPUs used to (and I think still do) speed division and multiplication via lookup tables. Also, checkout FPGAs for that technique on steroids.

I hope this helps...
Black Lives Matter
neuraleskimo
 
Posts: 195
Joined: 2019-03-12 23:26

Re: Abstractions and Performance

Postby LE_746F6D617A7A69 » 2020-05-10 19:07

For a while I was considering whether it's a good idea to answer Your post or not ... i.e. should we start another flame war between enthusiast of C and C++ ?
Normally I would ignore such a post, but it's so blatant that I've decided to post a reply:
There have been a number of discussions here and elsewhere on the Internet about abstractions, performance, and C vs C++. Today, I was considering a few implementations and found a really good example.

1. This is a really BAD example for showing differences between C and C++:
You're using Standard Template Library to show that for a fixed size array of <long> in C++ the compiler is generating the same code as for the fixed-size array of <long > in C mode -> this obviously has nothing to do with the "Abstraction vs Performance" topic.
2. You're showing that there are "exceptional" cases where the performance of C++ is the same as the performance of C -> *But* You should also mention that all Your "non-published" tests have failed to prove your aforementioned claim.
3. I'm assuming that You've never heard about the VMT (Virtual Method Table, vtable) - this is the main performance killer for C++ applications

To verify the difference in performance between C and C++ You should prove that using common features offered by C++ is not causing performance loss, that is:
- STL array of objects/classes
- Compare algorithms which are using objects/classes/derivation/polymorphism, etc..

In *My* experience, C++ produces a code which is unreliable, because the *glue code* which is responsible for implementing object-oriented functions is highly dependant on the compiler type/version - so basicaly it's unpredictable (regressions are possible)

I need my code to be predictable, no matter what type/version of the compiler is used.

If You absolutely need to prove that C++ is generating a code which is better/faster than the code generated by C, then You have to implement some complex algorithm in both languages and compare the performanece....

My experience shows that:
1. C++ produces typically 20-30% slower resulting executables (libboost included)
2. C++ produces huge executables in debug mode (typically 10 times bigger than in C)
3. Because C is several times faster in typical cases, I'm writing my algorithms as a C-library functions, which are then used by C++ GUI Front-ends

Regards.
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed
LE_746F6D617A7A69
 
Posts: 394
Joined: 2020-05-03 14:16

Re: Abstractions and Performance

Postby neuraleskimo » 2020-05-10 21:02

LE_746F6D617A7A69 wrote:For a while I was considering whether it's a good idea to answer Your post or not ... i.e. should we start another flame war between enthusiast of C and C++ ?
Normally I would ignore such a post, but it's so blatant that I've decided to post a reply:
There have been a number of discussions here and elsewhere on the Internet about abstractions, performance, and C vs C++. Today, I was considering a few implementations and found a really good example.

1. This is a really BAD example for showing differences between C and C++:
You're using Standard Template Library to show that for a fixed size array of <long> in C++ the compiler is generating the same code as for the fixed-size array of <long > in C mode -> this obviously has nothing to do with the "Abstraction vs Performance" topic.
2. You're showing that there are "exceptional" cases where the performance of C++ is the same as the performance of C -> *But* You should also mention that all Your "non-published" tests have failed to prove your aforementioned claim.
3. I'm assuming that You've never heard about the VMT (Virtual Method Table, vtable) - this is the main performance killer for C++ applications

To verify the difference in performance between C and C++ You should prove that using common features offered by C++ is not causing performance loss, that is:
- STL array of objects/classesand
- Compare algorithms which are using objects/classes/derivation/polymorphism, etc..

In *My* experience, C++ produces a code which is unreliable, because the *glue code* which is responsible for implementing object-oriented functions is highly dependant on the compiler type/version - so basicaly it's unpredictable (regressions are possible)

I need my code to be predictable, no matter what type/version of the compiler is used.

If You absolutely need to prove that C++ is generating a code which is better/faster than the code generated by C, then You have to implement some complex algorithm in both languages and compare the performanece....

My experience shows that:
1. C++ produces typically 20-30% slower resulting executables (libboost included)
2. C++ produces huge executables in debug mode (typically 10 times bigger than in C)
3. Because C is several times faster in typical cases, I'm writing my algorithms as a C-library functions, which are then used by C++ GUI Front-ends
Regards.


You seem to be a bit angry and I am sorry if I upset you. However, I think you focused on the wrong sentences. These are the ones I think are most important:
neuraleskimo wrote:When someone says, why can't I use macro, pointers, hand-rolled data-structures, etc. You can (I'll come to that in a moment), but really only as a last resort or with really good reason. Frankly, the world is full of buggy code. Please don't contribute to it. Be safe first, measure second, and use the sharp knives when necessary.

Do you not think that is good advice? I keep watching CVE after CVE involving pointer and memory issues (e.g., free after use). I also keep seeing people saying that they must use some dangerous feature. Sometimes (as I freely admit in the quoted sentence above) that is true, but in a great many instances it is not. The purpose of my post is not C++ is better than C; it is to encourage people to write safe code first and only write dangerous code as a last resort. I am not sure why that is controversial.

LE_746F6D617A7A69 wrote:3. I'm assuming that You've never heard about the VMT (Virtual Method Table, vtable) - this is the main performance killer for C++ applications

I think you are too angry and condescending here. Yes, I am well aware of v-tables. I never claimed that C++ was better than C, I only said that there are many abstractions that are no worse than older approaches and showed one. Period. In fact, I never mentioned C other than C vs C++ is a common argument on the Internet. Period. I even sorta admitted to using dangerous features myself (but only when necessary).

LE_746F6D617A7A69 wrote:You should also mention that all Your "non-published" tests have failed to prove your aforementioned claim.

Again, too angry and condescending. Again, my only aforementioned mentioned claim is "there are many abstractions that are no worse than older approaches and showed one." You also seem to be under the assumption that I care about the whole C vs C++ religious war. I don't. I freely write pure C code when needed. I kinda said as much. *shrugs*

By the way, the point of my example is that it is simple and easy to follow (both the code and the assembly).

Look, I gave you some code and compiler output. You gave me nothing. If you want to argue, you will be arguing with yourself. If you want to have a debate, then give me some code and output from compiler explorer (https://godbolt.org/) or similar. However, you need to know that I am well aware of areas where C can produce fast code than C++ and vice-versa. Yes, in some instances C++ can produce faster code than C.
Black Lives Matter
neuraleskimo
 
Posts: 195
Joined: 2019-03-12 23:26

Re: Abstractions and Performance

Postby cuckooflew » 2020-05-10 22:20

by neuraleskimo » Period. In fact, I never mentioned C other than C vs C++ is a common argument on the Internet

Yes it is, and I think you have found/attracted someone that enjoys the argument.
Perhaps LE_tomazzi would also enjoy"the great goto controversy" as well? :mrgreen:
Please Read What we expect you have already Done
Search Engines know a lot, and
"If God had wanted computers to work all the time, He wouldn't have invented RESET buttons"
and
Just say NO to help vampires!
cuckooflew
 
Posts: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Re: Abstractions and Performance

Postby neuraleskimo » 2020-05-11 00:50

cuckooflew wrote:Perhaps LE_tomazzi would also enjoy"the great goto controversy" as well? :mrgreen:

:lol: Very well played, cuckooflew!
Black Lives Matter
neuraleskimo
 
Posts: 195
Joined: 2019-03-12 23:26

Re: Abstractions and Performance

Postby LE_746F6D617A7A69 » 2020-05-11 04:27

Look, I gave you some code and compiler output. You gave me nothing.

Actually, You've posted a trivial code snippet to support Your false conclusions / claims...

Anyway, I have published few of my projects, and You can find most of them on SourceForge.net, f.e.:
https://sourceforge.net/projects/simpledcs/

@cuckooflew:
FYI: "LE" stands for Little-Endian ;)
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed
LE_746F6D617A7A69
 
Posts: 394
Joined: 2020-05-03 14:16

Re: Abstractions and Performance

Postby neuraleskimo » 2020-05-11 16:02

LE_746F6D617A7A69 wrote:
Look, I gave you some code and compiler output. You gave me nothing.

Actually, You've posted a trivial code snippet to support Your false conclusions / claims...

Anyway, I have published few of my projects, and You can find most of them on SourceForge.net, f.e.:
https://sourceforge.net/projects/simpledcs/


For a person who started off saying you didn't want to argue, all you do is argue and make unfounded assertions. I am sure your code is fine, but the point of the post is safe abstractions and performance. If you want to say I am wrong, you need to put forward some C++ code where the safe abstraction is worse than the unsafe approach. You haven't done that. *shrugs* Either contribute to the intellectual content of this thread or stop wasting our time.
Black Lives Matter
neuraleskimo
 
Posts: 195
Joined: 2019-03-12 23:26

Re: Abstractions and Performance

Postby LE_746F6D617A7A69 » 2020-05-11 17:21

I've already explained why Your "discovery" is just a special case - It's You who should prove that Your claim is true (i.e. that using C++ abstraction level does not affect the performance of the resulting code).

Judging by the fact that You didn't even know why Your examples are producing the same resulting code, I think that indeed further discussion would be just a waste of our time.

One last word: No matter what language is used, it's always possible to write unsafe code - this is guaranteed for every language that is turing-complete - the only difference is the abstraction level on which the unsafe logic is applied.

.
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed
LE_746F6D617A7A69
 
Posts: 394
Joined: 2020-05-03 14:16

Re: Abstractions and Performance

Postby cuckooflew » 2020-05-11 20:05

@cuckooflew:
FYI: "LE" stands for Little-Endian

Well, thanks, I was wondering about that part. I am not much of a programmer, actually would not even call my self one, but the "safe code", I do agree , it is important to use the language and code that is safest, so any way, carry on , it is kind of a interesting discussion ....thank you both for sharing your thoughts, code, and opinions...
neuraleskimo » Very well played, cuckooflew!

Ahh, thanks as well,...
Please Read What we expect you have already Done
Search Engines know a lot, and
"If God had wanted computers to work all the time, He wouldn't have invented RESET buttons"
and
Just say NO to help vampires!
cuckooflew
 
Posts: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Re: Abstractions and Performance

Postby LE_746F6D617A7A69 » 2020-05-11 20:32

cuckooflew wrote:
@cuckooflew:
I am not much of a programmer,

Yeah, it's obvious - so why the hell You do feel to be competent in judging cases beyond Your scope of competence?
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed
LE_746F6D617A7A69
 
Posts: 394
Joined: 2020-05-03 14:16

Re: Abstractions and Performance

Postby cuckooflew » 2020-05-11 23:58

I never judged anyone, but now I will, you are a troll, and I have no more interest in your posts.
I was polite enough, I only shared some thoughts, and the comparison to the Great goto controversy, perhaps you never heard of that and don't know what 'goto' is, it seemed like you would, actually bringing goto into the discussion, is another example of abstractions and performance, and all though goto can be used safely, generally it is not recommended because so many ignorant programmers use it wrongly.
Usually the "ignorant" programmers never learn because they think they all ready know every thing, but that is another topic.
But again, in no way am I judging anyone, I did find the thread interesting and was interested in both cases, But you seem determined to de-rail and trash this thread.
Actually your first post was a warning:
by LE_746F6D617A7A69 » 2020-05-10 19:07
For a while I was considering whether it's a good idea to answer Your post or not ... i.e. should we start another flame war between enthusiast of C and C++ ?
Normally I would ignore such a post, but it's so blatant that I've decided to post a reply:

" i.e. should we start another flame war between enthusiast of C and C++ ?" You decided you should, and odd that you say "we", is there someone else involved, EG: maybe bot, referring to it's creator ??? (Yes now I am judging, and you failed the turing test )
Nothing wrong with pointing out that a example is bad, or not as good as another, but so far you have not posted anything to show you can give a better example, all you have done is argue, and posted a link to some code on source forge, Is that really something you wrote, and the code as well ? Can you prove you are the author, and it is your account ? ...Don't bother to answer, I will not be reading your replies, and I do hope you stop trolling, but if you don't, I hope a admin just bans you, (not likely, almost anything is accepted here), Show us a better example then the one neuraleskimo showed or shut up, and I mean a example posted here, not some random link to source forge. BYE !
Just click YES
Please Read What we expect you have already Done
Search Engines know a lot, and
"If God had wanted computers to work all the time, He wouldn't have invented RESET buttons"
and
Just say NO to help vampires!
cuckooflew
 
Posts: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Re: Abstractions and Performance

Postby Head_on_a_Stick » 2020-05-12 17:36

Tomazzi is no troll, they are a forum member with some history here (albeit under a different user name). They do have a somewhat abrasive style but their posts tend to be of high quality. And I'll stop adding noise now before they tell me off (again).
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12650
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Abstractions and Performance

Postby cuckooflew » 2020-05-12 17:55

Ok,fair enough, I will apologize for some of what I said, I do see they use the name vtomazzi:
SimpleDCS
Status: Alpha
Brought to you by: vtomazzi

So my statement:
posted a link to some code on source forge, Is that really something you wrote, and the code as well ? Can you prove you are the author, and it is your account ? .
So I suppose they are the author,...any way, me to , I will shut up " now before they tell me off (again).",
Tip: Maybe if WE all cool off and stop being arrogant, abrasive, this discussion can continue , but in a more civil manner, I sincerely apologize for adding to a flame war,...I am more interested in the reasons C is a better choice in some cases, but like wise, the reasons C++ is a better choice in other cases, really, I am not judging either case, obviously I don't know enough to do that, but I would like to learn more, about both cases..if tomazzi does not want accept my apology, no matter, I do have better things to do anyway....thanks H_O_A_S for telling us more... :D
Please Read What we expect you have already Done
Search Engines know a lot, and
"If God had wanted computers to work all the time, He wouldn't have invented RESET buttons"
and
Just say NO to help vampires!
cuckooflew
 
Posts: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Re: Abstractions and Performance

Postby LE_746F6D617A7A69 » 2020-05-13 13:42

@ HOAS: thank You.

@cuckooflew :
I've never supposed that I'll have to prove that my code is mine...
If You think that I'm a liar, there's a simple way to verify Your claim:
Create an account on the SF, then You will be able send me a message. If I get the message, then obviously the SF account is mine, and so the code.
Of course there can be one problem: I will need You to confirm that the received message valid - what if You are a liar?

Fortunately, there's also other method:
Go to the project's page again and check the last blog entry :)
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed
LE_746F6D617A7A69
 
Posts: 394
Joined: 2020-05-03 14:16

Re: Abstractions and Performance

Postby cuckooflew » 2020-05-13 18:33

From the source forge link:>LE_746F6D617A7A69: hello, cuckooflew ;)

Ok, I see, and sincerely do apologize, .....also I want to say, to clarify, I do also use "goto" quite a bit myself, all though I am not a programmer by profession, as a hobby I do write some programs for my own use, but obviously no where close to being a expert,... any way, I will not interrupt this any further, just wanted to say in no way did I intend to offend anyone, but my apology if I did,...some times I am not very clear, and often people misinterpret , what I think is a funny joke, others get mad or offended,,
take care, and have a good day/night
Please Read What we expect you have already Done
Search Engines know a lot, and
"If God had wanted computers to work all the time, He wouldn't have invented RESET buttons"
and
Just say NO to help vampires!
cuckooflew
 
Posts: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

fashionable