If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.
The Internet - where men are men, women are men, and teenage girls are FBI agents!
I'm the least you could do
If only life were as easy as you
I'm the least you could do, oh yeah
If only life were as easy as you
I would still get screwed
Now the REAL question - which is faster on today's architectures:
i < n
or
i + n
Answer at 11.
- Gurm
The Internet - where men are men, women are men, and teenage girls are FBI agents!
I'm the least you could do
If only life were as easy as you
I'm the least you could do, oh yeah
If only life were as easy as you
I would still get screwed
int i, n = -20;
for (i = 0; i < n; i--)
printf("-");
I never did C nor C++, but if you use the first solution by Gurm, don't you run into the problem that "n" is not declared as a variable?
Join MURCs Distributed Computing effort for Rosetta@Home and help fight Alzheimers, Cancer, Mad Cow disease and rising oil prices.
[...]the pervading principle and abiding test of good breeding is the requirement of a substantial and patent waste of time. - Veblen
Nope. The way C declarations work, a comma-separated list of variable names can be called between the type (int) and the semi-colon.
Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.
Again, the loop will never run even once, since i will NEVER be less than n at the start.
- Gurm
The Internet - where men are men, women are men, and teenage girls are FBI agents!
I'm the least you could do
If only life were as easy as you
I'm the least you could do, oh yeah
If only life were as easy as you
I would still get screwed
All depends on who does the coding. I don't trust a compiler to give me "fastest."
Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.
I think he's talking about clock cycles.
You need to look up the op codes for the processor and find out how long cmp takes compared to add and etc.
It may also depend on whether the machine compile puts the values in registers or not before doing the operations.
chuck
For i+n to be faster it also relies on the Zero Flag being set by the addition operator (not sure with x86, believe Z80 didn't set it) - otherwise you would code something like this at best:
ahh, so its is a "while", not a "until"? Thx, just learned my first bit of C...
Join MURCs Distributed Computing effort for Rosetta@Home and help fight Alzheimers, Cancer, Mad Cow disease and rising oil prices.
[...]the pervading principle and abiding test of good breeding is the requirement of a substantial and patent waste of time. - Veblen
It's neither. It's a FOR, not a WHILE. There is no "until".
There's for, while, and do... while.
- Gurm
The Internet - where men are men, women are men, and teenage girls are FBI agents!
I'm the least you could do
If only life were as easy as you
I'm the least you could do, oh yeah
If only life were as easy as you
I would still get screwed
It's neither. It's a FOR, not a WHILE. There is no "until".
There's for, while, and do... while.
- Gurm
I hear what Umf is trying to say. Yes Umf, it's more like a "while" than like an "until." In C, a "for" loop is tested at the beginning of each execution, and may not even execute once if the condition is never true. A "do{}while" is always executed at least once, and is tested at the end of execution to see if it's going to run again.
Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.
It's neither. It's a FOR, not a WHILE. There is no "until".
There's for, while, and do... while.
- Gurm
The word may be "for", but c "for" loops are realy "while" constructs.
The increment is optional and there is not even an implied "next".
By that I mean that, while it is common to use x++ or -- (or ++x, --x if you realy want to get confused while debuging), they are not part of the for syntax of "for"at all.
In fact, unless I'm not remembering correctly*,
for(i=x; i<j;--i)
would decrement i even if j<i , at least on old compilers
I tried c once, after typing the program hello.c I simply stopped. Still glad I did.....
Thx all
Join MURCs Distributed Computing effort for Rosetta@Home and help fight Alzheimers, Cancer, Mad Cow disease and rising oil prices.
[...]the pervading principle and abiding test of good breeding is the requirement of a substantial and patent waste of time. - Veblen
Comment