[C] swapping vars

Feel like posting Off Topic? Do it here.

Moderator: MaxCoderz Staff

User avatar
hop
Extreme Poster
Posts: 378
Joined: Sat 09 Dec, 2006 3:42 pm

Post by hop »

Hey I know, how about improving a routine that is already optimal at a high level with low level optimisations as well?
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

That's my point remember..
Do you know there is a sorting algorithm that actually sorts in O(n+d) time (always - best and worst case) d being (array.max - array.min)? downside is that it takes d*element_size memory and that it only works if each element is unique. It's better than qsort if you know all elements to be unique and if you have the space (so practically never).
When an array is almost sorted and short in length then Shellsort could actually out-perform qsort, but how would you know an array is almost sorted? Typically you don't, and if you try to find out then you'd lose so much time that it isn't worth it at all - especially not if the outcome is that it isn't "mostly sorted" anyway.

So, the conclusion of that is: just use quicksort.
So why not do some low-lvl optimisation aswell? Once you compile your program, no one is going to run it on a different processor than you compiled it for because they can't, so you might aswell super-optimize it for that processor.

And we all know the story behind RIS processors don't we? They exist only because compilers are too lazy to utilize the full instructionset of CIS processors. Obviously this must be true, otherwise the SPARC would never have existed.
conclusion: compilers aren't that smart after all, in fact, they don't think at all, they pretend to be smart by using build-in tricks, but they're there because the programmer made it so, and that programmer isn't infallible - so that leaves some tricks unused and makes the compiled program less optimized than it could have been.

why conventional swap is ugly: it declares a variable that you do not need.
why it doesn't matter using 3 xor's: the compiler knows you mean a swap.
why you should add comments: someone may come along, read your code, and fail to understand it without comments.
imo, this leaves no reason not to use 3 xors

but it makes me wonder.. why do high level languages not have a swap keyword? or even as operator? a<->b maybe?
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

King Harold wrote:Once you compile your program, no one is going to run it on a different processor than you compiled it for because they can't, so you might aswell super-optimize it for that processor.
Not entirely true. Different CPUs of the same basic type (eg, "x86-compatibles") can have different extensions (MMX, 3DNow!, SSE et al) that are incompatible.

The way to resolve these differences are intelligent JIT compilers that take advantage of a particular configuration's hardware.

I, for one, hope that the days of native applications are numbered. ;)
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

I, for one, hope that the days of native applications are numbered.
So do I, but those JIT compilers will have to be quite intelligent, and not give cryptic errors as they currently do.
Not entirely true. Different CPUs of the same basic type (eg, "x86-compatibles") can have different extensions (MMX, 3DNow!, SSE et al) that are incompatible.
If they do not support some extension that you are using then they won't run the program - they could try, but fail (at least at some point). Too bad for them.

JIT compiler could solve that, but they'll have to be pretty damn good to be worth the overhead, otherwise they'd only be good for the programmer and not for the user - it has to be good for both ofcourse.
Personally I wouldn't mind running a program that is super-optimized for my own processor, in fact I'm always quite happy when I see that someone took the effort to make such a version.
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

King Harold wrote:conclusion: compilers aren't that smart after all, in fact, they don't think at all, they pretend to be smart by using build-in tricks, but they're there because the programmer made it so, and that programmer isn't infallible - so that leaves some tricks unused and makes the compiled program less optimized than it could have been.
Oh my dear, please do just a little bit of research before passing the verdict... Modern compilers are packed with advanced algorithms.
King Harold wrote:why conventional swap is ugly: it declares a variable that you do not need.
Why the xor swap is ugly: it adds three unnecessary operations. :P
King Harold wrote:why it doesn't matter using 3 xor's: the compiler knows you mean a swap.
It doesn’t matter for this particular implementation of this particular operation. But if you extend this attitude to implementing more complicated algorithms, you’ll end up worse off.
King Harold wrote:why you should add comments: someone may come along, read your code, and fail to understand it without comments.
If a simple operation like swapping two variables requires comments to be understood, you are on the wrong way...
King Harold wrote:but it makes me wonder.. why do high level languages not have a swap keyword? or even as operator? a<->b maybe?
Quite a few (especially interpreted ones) do, even ancient ones like CLU; it’s normally written as ‘a, b = b, a’ or something along those lines.
benryves wrote:The way to resolve these differences are intelligent JIT compilers that take advantage of a particular configuration's hardware.
Open source. ;)
benryves wrote:I, for one, hope that the days of native applications are numbered. ;)
You’re thinking in terms a very narrow application area then. :P
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 »

This thread spiraled out of control long ago. Sorry but its nothing but a debate thread in disguise -- degraded into an infinite loop of 'quote+re-enforce opinion'.

CoBB has the last word.
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
Locked