Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^7: quickness is not so obvious

by LanX (Saint)
on Jan 24, 2015 at 07:48 UTC ( [id://1114343]=note: print w/replies, xml ) Need Help??


in reply to Re^6: quickness is not so obvious
in thread quickness is not so obvious

I think the first time I saw an increment in a high level language was in a loop like

 for (j = 0; j < count; j++) { }

The existence of ++j was only a side node without immediate use case.

So maybe blame K&Rą for this implementation documentation causing a bias ultimately leading to an "it looks better" perception? (well most probably it even predates C ˛)

Anyway optimizing for such implantation details can bite you severely when processor technology advances.

For instance linearizing loops used to be a good idea till the memory access for the longer code was beaten by inline caches being able to hold the smaller loops in processor memory.

So who knows what comes next?

Personally I don't like post increment since it's a side effect sometimes causing weird undefined conditions, and in the world of concurrency side effects are the worst sin.

But in Perl at least I have to trust that it's optimized away in void and loop context.

Cheers Rolf

PS: Je suis Charlie!

updates

ą)

I found the oldest version in the net and it explicitly says:

This slightly more ornate example adds up the elements of an array: sum = 0; for( i=0; i<n; i++) sum = sum + array[i];

But actually ++i would work the same, this post-increment notation hides the fact that the increment is only executed at the end of the loop's body.

NB: like in Perl

For Loops Perl's C-style "for" loop works like the corresponding "while" +loop; that means that this: for ($i = 1; $i < 10; $i++) { ... } is the same as this: $i = 1; while ($i < 10) { ... } continue { $i++; }

˛) according to WP it was invented for B.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1114343]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found