- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
People who write assembly code know better than this. They know that a for-loop counter runs in the CPU registers.
This C code doesn't use a memory location to store i:
register int i; int sum term[N];
for (i = 0; i < N; i++) { sum += term[i]; }
That longjmp() guy doesn't know anything about computer architecture!
Admin
All branching constructs (if, for...) boil down to GOTOs on the chip. Without some sort of jump or GOTO you could not have any branching whatsoever. Just straight through execution, boring.
When assembly was the high level language you used something like a GOTO because the chip had a correlating jump, goto or "set program counter" type instruction. Of course this only existed because you had conditional jumps to begin with. When you program assembly sometimes depending on which chip, you may have an operation that sets a flag followed by an instruction that jumps if the flag is or isn't set. GOTO has since lived on into higher languages in some forms.
On one chip I programmed it had two very different jumps. One was for nearby jumps within like +-127 instructions and a long jump for going further. I'd bet that the long jump in this WTF is a throwback to that other kind of jump.
I had a feeling the WTF stems from a guy who used lots of long jumps in assembly and just wanted the same thing in C even though it was out of place. But since this is from 05, I figure this is an idiot who copied off classmates to graduate and in the real world misapplied an idea from a Google search result.
There are tons of ways different chips do things and so we become grateful that our forebears invented something so simple, high level and usable like C, with all its parts that may be considered quirky or outright annoying. The way a computer does things can be amazingly boring and repetitious in reality. The compiler helps hide the fact that when you ask for a screwdriver and later a chisel it actually gave you the same thing. It might even be executing the same instruction for you IFs, FORs and WHILEs(dare I say SELECT CASE too?)
In the end trust the compiler, learn the language, don't hold on to the parts of your past that seemed smart then, get to work, less whining, more doing, improve your skills or be passed-by/laughed-at.
Admin
I'd have asked for 1% of whatever it saved them, ongoing.
And then retired.
To sit on a gold throne.
On an island made entirely from beer, money and women.
Admin
Admin
And nor, far more importantly, does it do so if register pressure means that something has to be spilled to memory, and the compiler determines that the best thing to spill is the loop counter. And it'll do that on the basis of considering all the options and the costs of spilling various different variables to memory and the number of times they'd need loading back and the changes that will cause to the instruction stream and the costs of those differences. And it'll do it much more comprehensively and accurately than if you hand code it, and it'll do it almost instantaneously.
There's a time and a place for hard-core low-level coding, but the age of the hotshot cowboy coder is long gone, and the Story of Mel is a warning from history these days, not a model to look up to and emulate.
And you appear to be under the misimpression that the 'register' keyword does anything at all. Not this millennium it doesn't.Admin
The only place I've seen it used was one where it was better to demolish the work crew than build an even slightly flawed road - insanely complicated chip-design processing software. If it hit something it just couldn't handle (and it had a hell of a lot of oddity-handling code), it was better to abort the whole thing than generate a possibly flawed photomask.
Admin
My point is that you can't just blindly believe that a O(n) is faster than O(n^4) without analysis of the problem in hand and how the algorithms actually perform in practice.
Admin
Admin
I have seen longjmp used in an application that was translated from old Fortran to C. One particular Fortran module was so tangled with GOTOs that it became necessary to use longjmp...or redesign the code entirely, which wasn't scoped effort.
Admin
Money? WTF do you need money for, in this situation?
Admin
Aw geez... I wish I had known that earlier! But it's never too late to reform; from now on I'll use "while(1)" for all my loops instead :^)
(CAPTCHA: genitus. Hmmm...)
Admin
Addendum (2009-01-12 15:35): EDIT: Also, I might need to buy a cushion. Solid gold is not always the comfiest thing to sit on.
Admin
The only time I ever used it was in my operating systems class in college... we were creating a toy multitasking operating system, and setjmp()/longmp() were necessary as part of the routine that would switch the processors' context from one running one thread to running another thread.
Admin
Lemme guess, you have only used GNU compilers. You are far from the truth, and some people on here work for high quality compiler vendors.
Admin
I think he just want to attract a bunch of velociraptors instead of one, so they start fighting instead of eating him...
Admin
Admin
uhhh, it outperforms it as input goes to infinity. It doesn't "always" outperform it.
If I know my input n is limited to 1000 I would much rather use O(n^2) than O(48395823598723log(n))
While using a faster theoretical algorithm is the better option is the vast majority of cases there are times when it's better to use a lower overheard "slower" algorithm.
Admin
I don't think it is required to divide a bool by 0 in order to break the loop.
Admin
Amateur. Real men rewrite their loops as recursive functions.
Admin
Real men use call/cc.
Admin
Your milage may vary. The register qualifier is more of a recommendation than a set-in-stone command. The compiler is free to ignore it (which is why programs will compile even if you set every variable in existence to "register").
Also, are there CS programs that don't require even basic assembly programming? I mean, I go to really shitty college (a Java school, even), and even we do some assembly on a toy machine.
Admin
Admin
I found some performance enhancements on the man pages, and now my jump is much longer!
Admin
Not to mention that 'true' still sounds pretty constant to me.....
Admin
not familiar with the 'while etc' construct - please elaborate
Admin
Oh, THAT sort of throne.... in that case, where do you pee on this island?
(Reminds me of the fisherman who were granted some wishes. The first wished the sea would be turned to beer. The other hits him over the head saying "you idiot, now we're gonna have to pee in the boat")....
Admin
Admin
In twenty two years of C programming (O Lord, take me now. Is it that long? Why does the fucking thing keep coming back to me, like a dachsund with an unexpressed anal gland?), I have only ever once come across a sensible use of longjmp.
Consider: longjmp trashes the entire stack (no garbage collection here, friend) all the way down to its target.
Consider: When would you want to do this?
An inventive cow-orker came up with the only possible reason, as an application (not a systems) programmer: when you're implementing a licence key, and you want to let the operating system clean up at the point where the licence key fails.
I imagine he had some smart way to prevent disassembly (which would be OS-dependent).
In the mean time, do not click on this link. Or use longjmp.
Addendum (2009-01-12 17:47): Strictly speaking, it's a "Men Page." Don't go there, irregardless.
Admin
Admin
Admin
Others have responded, but haven't really explained:
Libraries need to handle the general case. Therefore, the compiler cannot optimize them as much as it can your individual code. That's ok, because most compilers don't bother with the optimizations that would change based on whether the code was for a library or not.
Libraries tend to be faster because they're generally written by someone who knows what they're doing. Libraries that are written poorly tend to get re-written more frequently, until eventually they are written well. Libraries will still tend to have a general case limitation, however, as they need to handle situations your specific code may not need to handle.
The only reason why a correctly functioning library would have faster code than your code is that you don't know as much as the person who wrote the library.
The real point, however, is that most of the time, programmer skill isn't significant, other than selection of algorithms: the compiler will do a better job for your code than you will. So, let the compiler do its thing.
Only if the compiler doesn't do a good enough job should you go back and optimize. And, as others have said, it's not optimization if you don't actually measure the before and after speeds.
Admin
Back in the day, I had to work with some code which was written with longjmp() for speed.
Initially, I converted a copy to use a cascading fallout condition, and showed a performance comparison between the two: the longjmp() version took several hundred times longer to abend.
Then the original programmer (who happened to still be around - that doesn't happen often) showed me my error:
It's not that longjmp() abends quicker. It's that it doesn't degrade the typical case nearly so much. When the program in question runs without abending 99.99% of the time, taking 100 times longer to abend can still be quicker on average than taking a 0.01% performance hit the rest of the time.
(For the pedantic - consider a program with a normal runtime of 10 minutes. The time to abend is 100 milliseconds with longjmp() and my version took 1 millisecond. A 0.01% increase in runtime over 10 minutes is 60 milliseconds. It only takes two runs without hitting longjmp() to make up for one run that hit longjmp().)
Now, for the curious, after a few months, we still went with my version, because the longjmp() version occasionally abended horribly, due to the way it would totally fail to clean up resources that were allocated after the setjmp() call. One of those hit while the manager still remembered the little performance debate. So he asked the question, "Would this other version have caused the same issue", and got the answer "no."
Admin
Admin
[quote user="Anon"](Reminds me of the fisherman who were granted some wishes. The first wished the sea would be turned to beer. The other hits him over the head saying "you idiot, now we're gonna have to pee in the boat").... [/quote]LOL, now why does that sound like an aussie joke? :)[/quote]
The joke is that if the ocean were made of Aussie "beer" it could only be improved by pissing into it. ;)
Admin
Dude, I think you need to spend some time away from the machine, and maybe learn some people skills.
Admin
How unfortunate that none of these high quality compiler vendors provide compilers for the platforms I have worked on.
Admin
Ironically of course qsort is itself quadratic worst-case :P
Admin
Ironically of course qsort is itself quadratic worst-case :P
Admin
Admin
there was an old IOCC winner that consisted of a single (multi-page) line, comma'd together, which used setjmp/longjmp for all its control flow.
Admin
For those of you who have been using Java too long, C has functions, not methods.
Admin
[quote user="Mcoder"][quote user="Why is DailyWTF more like DailyWTFWTF"][quote=JW] Library code is often faster than "user-optimized code", because the compiler can apply optimizations at the level of CPU/platform-dependent calls. [/quote]
I read these comments and it makes me realize you're all a bunch of C#/Java babies. None of you get close to the real the machine.[/quote]
Ok, I normally disagree when people say that, but we are talking about C here, you don't use it except when you need low level programming, so... I second that! No library will be as optimized and trimmed to your application as your own code.
[/quote] WTF? You use C for "low level programming"? I could cry.
Admin
They are if you choose a C, BASIC or PASCAL chip.
Note that "every chip" is a strong statement. You can also design processors without GOTO. For example a LISP machine.
Admin
I think the big thing is GOTO was overused and thus need to reigned in a bit. setjmp/longjmp, while they have their abuses, are less of a pervasive problem. Most C programmers have never used a setjmp and many don't even know the pair exists.
And this is probably a good thing. If you need them, research will eventually lead you to them.
Admin
AAAHHHHGGGGGGGGGGGG!!!!!! I was steeled for some tubgirl variant, but I was horribly horribly mistaken. Can I wash my forebrain out with soap??? The goggles - they did nothing!
Admin
Admin
return; might be a typo (forgot the return value)...
RETURN_NOTHING is unambiguous as to the intention.
Admin
Exception Handling I have seen them used for that
Admin
Admin