Beginner Exercises

Pages: 1... 45678... 16
Aye ya... and I guess you think a = 5+3; is slower than a = 8; because it has an extra addition, right?

There are some compilers that do not do this though


Name one.

AFAIK none exist*. I think you are just assuming that there is such a compiler.

And even if there WERE such a stupid compiler, one extra condition check would be the least of its problems.



(* I'm sure you could write such a compiler to spite me, or perhaps find the first ever C compiler or something equally obscure -- but in terms of fully functional compilers that more than 10 people actually still use today... I'm sure there are none)

If you'd like, go and try that with every compiler and see if it's the same as some will give different results.


You're the one who made the initial claim. And you've since just been spouting conjecture (or citing other people's conjecture).

Tits of GTFO =P

I tested on my machine. I got identical results. Why don't you test on yours and tell me what the results are.


EDIT:

I just want to clarify my point here.

I don't care if you want to use for(;;). That's great. I don't have any problem with that. Each programmer can have his own coding style, and that's a wondering thing.

My problem is when you start making baseless claims that your preference in coding style is more efficient than someone else's preference in an attempt to sway them into adopting your style.

If while(1) really did have an extra conditional check, that's one thing (but even then... talk about micromanaging)... but that's such an absurd notion it's almost laughable.
Last edited on
computerquip wrote:
There are some compilers that do not do this though

Even if they exist, they do not comply to the standard

C++ standards §6.5.3:

The for statement
for ( for-init-statement conditionopt ; expressionopt ) statement
is equivalent to
1
2
3
4
5
6
7
{
          for-init-statement
          while ( condition ) {
                   statement
                   expression ;
          }
}

[...]

Either or both of the condition and the expression can be omitted. A missing condition makes the implied
while clause equivalent to while(true).

disassembly taken from VC++ 2008:
for(;;);
0041138E jmp main+1Eh (41138Eh)
while(true);
00411390 mov eax,1
00411395 test eax,eax
00411397 je main+2Bh (41139Bh)
00411399 jmp main+20h (411390h)
do;
while(true);

0041139B mov eax,1
004113A0 test eax,eax
004113A2 jne main+2Bh (41139Bh)
Now try it with optimizations on.
Well I can honestly say I am surprised. This makes me want to check VS myself just to see with my own eyes (if only I could!)

I guess I must retract my earlier statement about such a compiler not existing. My apologies!
Why on earth would you need the 'most efficient' infinite loop?! Embedded systems or something?
Last edited on
Well, any program should strive to be as fast as possible.
For overall optimizations, I agree.

For micromanaging optimizations, I strongly disagree.

There was a time way back, when games had to do pixel work on a 320x240 display. They ended up doing this:

 
pixels[ (y << 8) + (y << 6) + x ] = foo;


The idea is that:

1
2
3
(y<<8)+(y<<6)  =
(y*256)+(y*64) =
y*320


But because multiplication used to be slow, having two bitshifts and an addition was faster, so games were coded this way for a speed boost.

The problem is... multiplication sped up. And ultimately the bitshifting route became the slower option. And while a compiler could easy compile (y*320) as (y<<8)+(y<<6) if it would determine there'd be a speed gain, doing the reverse isn't as obvious.

Bottom line: you're better off leaving these kinds of optimizations to the compiler. It has a stronger grasp of the bigger picture than you do (most of the time)

-----------------
That said...

for(;;) does not have any such detriment to it, so there's no reason you shouldn't be using it. So in that sense your point is strong, and I agree with you.
Last edited on
I thought bitwise shifting was still faster.

Damn emulator tutorials must be out of date >:(
1 shift is [probably] faster than 1 multiply. And in such an event, the compiler would recognize it. ie: foo *= 256 is likely to get compiled to foo <<= 8 if there's a speed difference.

But two shifts AND an addition? faster than 1 multiplication? Doubtful.

Besides... the computer/program is only as fast as the slowest link in the chain. Processers now are so bloody fast that you're going to get bottlenecked on something entirely different. You're much more likely to have speed issues with memory accesses than with computational numbercrunching.

Everybody these days seems to have at least a 2GHz processor, but how much RAM floating around actually has 2GHz access speed? Or 2GHz bus speed?
Well, according to the newest piece of reference I can find that includes clock counts, which is for 486, MUL using 32-bit operands can take as much as 42 clocks, while ADD takes no more than 3, and shifts take between 2 and 4 cycles.
If you can find a reference for a newer processor, however...
You're much more likely to have speed issues with memory accesses

I agree there. CPUs have left memory in their wake. The fastest clocked memory was DDR3 at 2GHz (still, thats ridiculously fast); but DDR3 timings are quite high. I think the best for DDR3 I ever saw was 9-9-9-24, which is horrible.

On the subject of memory, I just bought 2 GiB of DDR3 RAM at 1333MHz... Then, when I'm 16 and can get a job, I'll buy the rest of the system. Just £537 left to go (I bought a motherboard, too).

Link to 2GHz clocked memory: http://www.patriotmemory.com/products/groupdetailp.jsp?prodgroupid=131&prodline=5&group=PC3-16000%20-%202000MHz%20Memory%20Modules&catid=51

Features

* Extreme Performance PC3-16000 (2000MHz)
* EPP Ready
* Equipped with advance aluminum heat-dispersing shields
* 100% Tested and Verified
* Lifetime Warranty
* RoHS Compliant

Translation:

* it's overclocked to ridiculous levels
* this sounds official
* we put heatsinks on it
* we ran memtest86+ on it for half an hour
* it works
* again, this sounds official
Last edited on
* Lifetime Warranty
actually translates to: We guarantee this to work for at least a year, unless you make use of the over-clocking we have put in by default. In which case we guarantee this to work for 3months tops.

I envy you, at your age I was too busy smoking pot, skateboarding, and graphitting to worry about complicated things like studing programming... oh how I wish I could take it all back. Would have just been in time for the big boom ... :(
Last edited on
For those that want unique names, I have a census list from american most popular names in 1987 or some such:
Last edited on
GIRLSNAMES.TXT
Last edited on

I have however deleted from the text the number of times each name was used. Still this is a useful for name generator or some such.
If you would like the files with the number of occurances as well just pm.

Kinda sad Jesus is 67th on the list.... Why on earth would you name you boy that?,Or I supposed why would 3000+ americans name their son Jesus in 1 year...
Last edited on
closed account (S6k9GNh0)
omfg, fpaste it next time or something, holy crap.
Gee, thanks. That's what we needed. 1000+ lines of names to scroll down. With respect, you could have uploaded that to a site like pastebin, or this "fpaste" computerquip speaks of, which is presumably something similar.
whine whine, use your mice!
whine whine, use your mice!

But I don't have a mouse, what can I do? (!)
Pages: 1... 45678... 16