[C] swapping vars

Feel like posting Off Topic? Do it here.

Moderator: MaxCoderz Staff

King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

because it is optimized the same as a 'conventional swap', there's nothing wrong with the xor trick other then being "obfuscated" meaning that newbies have no clue what's going on.
Haven't you seen the whole ui looking like a flash app thing with all the online media info and stuff?
no? where? maybe that's in the newer version? DAP looks like that too..
Check if the HTT speed doesn't exceed 1000Mhz or 2000T for DDR2.
it should stop at 850, but I haven't checked if it really does so, however I never had any trouble with it.
but the first question should be whether we should enter them at all, and that's a problem on a much higher level.
sure but I never said that high-level optimization was bad, but imo low-lvl optimization (by hand) shouldn't be skipped altogether.
Are all of them deluded and the lone warrior is right?
they value development time more than performance.. Strange though, if you are paid per hour you'd better take as many hours as possible, no? withing certain limits ofcourse..
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

King Harold wrote:because it is optimized the same as a 'conventional swap', there's nothing wrong with the xor trick other then being "obfuscated" meaning that newbies have no clue what's going on.
That's most likely because the compiler writers are also aware of this trick and hardcoded it to be equivalent to the simple way of doing the same. But they obviously can't prepare for every case of 'cleverness' like that. Again, three XORs are not hard to read per se, but if you don't know the trick, it takes much more thinking time to realise what they do in the end. And when you look at an implementation of an algorithm you don't know, it's better be as clear as possible.

By the way, the quirkiness of the XOR trick is nicely illustrated by the following snippet:

Code: Select all

int a = 1, b = 2;
a ^= b ^= a ^= b;
What will be the value of a and b after execution if the language is C and what happens if it's Java and why?
King Harold wrote:sure but I never said that high-level optimization was bad, but imo low-lvl optimization (by hand) shouldn't be skipped altogether.
But don't you agree that optimisation is only necessary if performance is actually a problem? And that low-level optimisation should come as a last resort, after doing everything else to cure the problem?
King Harold wrote:they value development time more than performance.. Strange though, if you are paid per hour you'd better take as many hours as possible, no? withing certain limits ofcourse..
You're most likely bitten by deadlines (often set by brainless marketing drones) even if you do your work properly. You don't gain anything by sabotaging your own work...
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

But don't you agree that optimisation is only necessary if performance is actually a problem? And that low-level optimisation should come as a last resort, after doing everything else to cure the problem?
no, imo you should never produce anything that performs badly - whether the performance be needed or not.
What will be the value of a and b after execution if the language is C and what happens if it's Java and why?
3 in one language, 0 in the other?
Otherwise it wouldn't make much sense asking..
You're most likely bitten by deadlines (often set by brainless marketing drones) even if you do your work properly. You don't gain anything by sabotaging your own work...
I know. What about all those free programs out there then? They have all the time in the world to optimize them as fully as possible.
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

King Harold wrote:because it is optimized the same as a 'conventional swap', there's nothing wrong with the xor trick other then being "obfuscated" meaning that newbies have no clue what's going on.
Isnt that the reason you posted it?
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

King Harold wrote:no, imo you should never produce anything that performs badly - whether the performance be needed or not.
Am I not making myself clear enough? Why bother optimising when it doesn't perform badly in the first place?
King Harold wrote:3 in one language, 0 in the other?
Otherwise it wouldn't make much sense asking..
It says a lot that you don't even recognise your beloved XOR trick when presented in a little bit different form (but only in one of those two languages!). Thanks for demonstrating that it's indeed obfuscated. :P
King Harold wrote:I know. What about all those free programs out there then? They have all the time in the world to optimize them as fully as possible.
What about features, usability? Time should be put into them, not into performance gains that hardly make any difference.
coelurus
Calc Wizard
Posts: 585
Joined: Sun 19 Dec, 2004 9:02 pm
Location: Sweden
Contact:

Post by coelurus »

This thread is horrible, it's like a religion-related thread :) King Harold, I'd advice you to buy a can of development experience.

Oh snap, I posted yet again! Back to my GPU accelerated radar satellite image geocoding program that can geocode maps at 40 FPS compared to the regular seconds by professional software, in which the problem is not that the program hasn't been written by hand in asm nor that the map meshes do not have LODding, but that there is an absolute shift in slant range. *big hint*
User avatar
hop
Extreme Poster
Posts: 378
Joined: Sat 09 Dec, 2006 3:42 pm

Post by hop »

It may not be mature by your general definition but my comparison of who really isn't mature still stands in my opinion.
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

It says a lot that you don't even recognise your beloved XOR trick when presented in a little bit different form (but only in one of those two languages!). Thanks for demonstrating that it's indeed obfuscated.
you didn't prove anything, you obfuscated it this time by putting them together and asking a question hinting at a difference. I assumed it would be a difference in order-of-operation.. because you asked the difference between the 2. "Different forms" like that (jamming code together in 1 row) is far more obfuscating than neatly doing 1 thing per line..
What about features, usability? Time should be put into them, not into performance gains that hardly make any difference.
tons of features can easily make a program huge and slow and hard to use though

@coelurus: I really shouldn't reply to that ofcourse but it sounds terribly insulting and does in no way contribute to the discussion. so thanks.

@CoBB: I don't know any java (no really, none) so I can't really compare it C
Isnt that the reason you posted it?
no?

Code: Select all

a ^= b ^= a ^= b;
write as:
a ^= b;
b ^= a;
a ^= b;
instead
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

hop wrote:It may not be mature by your general definition but my comparison of who really isn't mature still stands in my opinion.
I don’t really understand what you’re trying to say with that, since no-one spoke against standing up for one’s opinion. But reiterating an opinion without backing it up with arguments is far from standing up for it.
King Harold wrote:you didn't prove anything, you obfuscated it this time by putting them together and asking a question hinting at a difference. I assumed it would be a difference in order-of-operation..
Assumed? I thought you knew C. Even if you don’t know Java you should have known (at least with the level expertise I thought you had based on your repeated newbie-bashing) that neither 0 nor 3 can come out of the C code.
King Harold wrote:"Different forms" like that (jamming code together in 1 row) is far more obfuscating than neatly doing 1 thing per line..
It performs exactly the same operations in exactly the same order. And again, here’s the cold hard proof that readability does matter, even on such a small scale. The one-line XOR code is less readable than the three-line one, and the latter is less readable than three assignments. You could only read the second one because you knew the trick already. If you hadn’t, it would have caused just as much trouble as the oneliner.
King Harold wrote:tons of features can easily make a program huge and slow and hard to use though
That’s why you have to put time into designing the application properly on the large scale instead of wasting it on minute details. And your blazing speed isn’t worth much when your program is full of bugs.
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Assumed? I thought you knew C. Even if you don’t know Java you should have known (at least with the level expertise I thought you had based on your repeated newbie-bashing) that neither 0 nor 3 can come out of the C code.
yea I know I though there were 4 like I said..
I know it's stupid..
That's what you get from obfuscating and asking for a difference eh?
And your blazing speed isn’t worth much when your program is full of bugs.
nope, I never you weren't allowed to debug first though did I
If you hadn’t, it would have caused just as much trouble as the oneliner.
nah, I was looking for a difference, so I found one, not a true one but well.. 3 lines are easier to follow, the assignment are a bit easier yes but I think most ppl will manage with the 3 line version of the xor way

if you wouldn't have implied that there was a difference I wouldn't have needed to make a fool out of myself but ok. It's not like my C is that bad (the decoder you can find in my new project thread is in C for example)
User avatar
hop
Extreme Poster
Posts: 378
Joined: Sat 09 Dec, 2006 3:42 pm

Post by hop »

CoBB wrote:reiterating an opinion without backing it up with arguments is far from standing up for it.
No it isn't.
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

King Harold wrote:That's what you get from obfuscating and asking for a difference eh?
I really did expect you to know the oneliner version. It’s a better known rendition of the trick as far as I could gather. And it’s also an interesting story why it doesn’t work in Java.
King Harold wrote:nope, I never you weren't allowed to debug first though did I
Of course not, the problem is that if you put too much emphasis on performance, you’ll end up breaking your design to achieve it, and that leads to more error prone code, hence more bugs.

It must be noted though that the other extreme, i.e. adding too much structure is equally harmful.
King Harold wrote:if you wouldn't have implied that there was a difference I wouldn't have needed to make a fool out of myself but ok.
Of course the goal was not to make a fool of anyone but to illustrate a point, even if I didn’t expect it to happen in such a direct way. Anyway, I hope you see it better now.
hop wrote:
CoBB wrote:reiterating an opinion without backing it up with arguments is far from standing up for it.
No it isn't.
Hmm, good one. :lol:
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

And it’s also an interesting story why it doesn’t work in Java.
then tell us :)

I can think of several possibilities, but it's probably none of them :P
reiterating an opinion without backing it up with arguments is far from standing up for it.
I've something to say about that aswell: it's not abandoning it either!
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

King Harold wrote:then look. The point is that all those lazy programmers who aren't doing their utmost best to deliver good programs should get their cup of coffee and start thinking about doing something usefull, like optimizing just a little bit more. Even if it's just milliseconds, it does matter.
I think you underestimate how much of it could actually be blamed on languages and libs used. Fact is people use Libs so they can produce thier code faster and give the users realistic release dates and actually FUNCTIONAL programs.
0xROWDY wrote:Back to the original topic, I believe alot of programmers have become lazy using the shiny new hardware available today.
Lazy isn't the right word here. If you think back to 20 years ago, the same hardware wasn't available, and optimizing low level with nothing better than 20mhz processors actually saved a practical amount of time. More limitations make things easier to optimize for...not code for but optimize sure.

I wouldn't say its lazy, I'd say they just changed their focus, since the need for low level optimization done by the coder has reduced.
reiterating an opinion without backing it up with arguments is far from standing up for it.
I've something to say about that aswell: it's not abandoning it either!
What? That's not a good thing. It's absolutely shameful in a debate where proof can be provided.


I think everything can be resolved right now if King Harold picks up another stick of ram, 512mb just doesn't do it anymore.
Image
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

King Harold wrote:I can think of several possibilities, but it's probably none of them :P
Here it is (15.7.1 is the relevant point):

http://java.sun.com/docs/books/jls/seco ... html#18498
Jim e wrote:I think everything can be resolved right now if King Harold picks up another stick of ram, 512mb just doesn't do it anymore.
Well, my Xfce desktop eats about 80 megs by default. When I get wild with Opera, it can go up to 120-130.
Locked