Something I bet you didnt know you could do with pointers #2

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by avansc »

those results are bogus. atleast i dont get that on my pc.

C++ algorithm swap 3063
C pointer swap 107344
C macro swap 250
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by bugmenot »

Are you compiling in release mode?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by avansc »

bugmenot wrote:Are you compiling in release mode?
oops my bad.

but i would make that macro just an XOR swap.

thats alot faster then either.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by avansc »

release build

C++ algorithm swap 1079
C macro swap 1062
// i removed the malloc swap.
C XOR swap 0


as you can see the XOR swap is alot faster. at least a thousand plus.\
and C macro is still faster. so im not sure where you get C++ templates are faster.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by avansc »

for ITERATIONS = 1000000000000000000000000

the XOR swap still has a finisTime - startTime = 0

where is std::swap....
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by bugmenot »

I said they are generally faster and should always prove that it is slower before rolling out your own swap implementation. (As a side note, I edited the previous post to include and XOR swap, I am surprised you got 0. I wonder if it is CPU chipset related :/).

In this case, the XOR swap is faster then the C++ standard swap function for ints. For non-base types such as a class or a struct that are large then sizeof(int), (if you can do an XOR swap with the raw data without problems), it is more likely to be slower then the C++ standard swap because of the wrap around code to actually do an XOR swap with the raw data and the fact you have to write your own XOR function to deal a data structure with more then sizeof(int) bytes.
Last edited by bugmenot on Sun Dec 07, 2008 8:37 pm, edited 1 time in total.
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by bugmenot »

avansc wrote:for ITERATIONS = 1000000000000000000000000

the XOR swap still has a finisTime - startTime = 0

where is std::swap....
1000000000000000000000000 doesn't fit inside an 32-bit int.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by avansc »

bugmenot wrote:
avansc wrote:for ITERATIONS = 1000000000000000000000000

the XOR swap still has a finisTime - startTime = 0

where is std::swap....
1000000000000000000000000 doesn't fit inside an 32-bit int.
yes i know, but i wonder what happens when you do something like

Code: Select all

static const int ITERATIONS = 100000000;

startTime = clock();
for( int i = 0; i < ITERATIONS; ++i )
{
    for( int j = 0; j < ITERATIONS; ++j )
    {
        for( int k = 0; k < ITERATIONS; ++k )
        {
            blah = blah ^ foo;
            blah = foo ^ blah;
            blah = blah ^ foo;
        }
    }
}
finishTime = clock();
std::cout << "C XOR swap " << finishTime - startTime << std::endl;

100000000*100000000*100000000 = how much again.

see thats what happens to people who just reply on templates and other C++ ribish. they become so rigid in their ways that they assume that if a number cant fit in a 32bit int. then, well we cant go any higher.

ps: im not saying that you dont know about nested loops. but mearly that you assume that when i say iterations > 32bit int. that i cant possible loop that many times. anyways. i think i made my point that C++ is dog slow compared to a C programmer who knows little tricks.

note that you can do this in C++ to. but if you are going to rely on templates you are in my opinion a sub par programmer.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
sparda
Chaos Rift Junior
Chaos Rift Junior
Posts: 291
Joined: Tue Sep 23, 2008 3:54 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by sparda »

avansc wrote:

note that you can do this in C++ to. but if you are going to rely on templates you are in my opinion a sub par programmer.
Dude, whats up with the condescending language? I guess Linus Torvalds was right when he said that programmers are not the nicest of people. :|
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by avansc »

sparda wrote:
avansc wrote:

note that you can do this in C++ to. but if you are going to rely on templates you are in my opinion a sub par programmer.
Dude, whats up with the condescending language? I guess Linus Torvalds was right when he said that programmers are not the nicest of people. :|
no im just not the nicest of people. ;)

but seriously, im not saying dont use it at all.

i guess cause im so closely involved with the education system (tertiary level) that i see how they are breeding programmers that are not equipped with the right tools and knowledge.

i bet you half the kids in college around america in computer science degrees do not know what a pointer is.
they dont know about little and big endian.
they only know what library functions already exsist. so if you pose a situations to them that their library cant do (probably just because the student doesent know how to impliment it in the scenario) they fall flat on their faces. and thats sad. and angers me.

but im pretty confident that mr/mrs bugmenot is a compitent programmer. and i do appologize. it was uncalled for. i just get mad because we get kids with CSC degrees (from good univirsities) that cant program worth shit. (there are kids starting with a 60K salary)
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by bugmenot »

Woah. Let's back up a second here and get some facts straight before this spirals anymore.

The two points I was trying to make in my first post of this thread are:
1. The C pointer swap function posted was not more efficient then a standard template swap or a 'store a temp variable, blah, blah'.
2. That you should try to avoid reinventing the wheel especially if it has already been implemented by the standard library because it is likely to be more robust and safer then your own implementation if you consider who moderates and implements them. And you should only implement your own if you can prove it is faster then one that is already written.

No where in this thread did I say 'C++ templates are faster then anything' or anything along those lines.

Now about your last post, fine, I forgot about nested loops. I naively assumed you did a check against such a large literal due to the way you wrote it in your post.

Templates are C++'s answer to C's macros. It provides a decent meta-programming layer to the language for generic programming, not speed (although they are developed to be as fast as they can while being generic) while providing better type safety then macros. Look at the Boost Library and the C++ Standard Library for instance, both of which relies heavily on templates so they can be used generically but provide such a large range features and utilities.

I am still intrigued by how your XOR swap got 0, on my Duo core MacBook, it is definitely not zero (with your code snippet, it was taking at least 30 secs before I stopped it myself).
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Something I bet you didnt know you could do with pointers #2

Post by avansc »

points noted.
oopsie, second mistake by me, hehe. thats not a XOR swap... hehehe.
i miss wrote it.

my bad. im just gonna go stick my head in a hole now.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply