for loop speed trick?

P

petermichaux

Hi,

In the Yahoo! UI event.js file I see the following quite a bit

for (var i=0,len=unloadListeners.length; i<len; ++i) {

when I always just write

for (var i=0; i<unloadListeners.length; ++i) {

Is it worth it to declare the variable len to save time evaluating
unloadListeners.length?

Thanks,
Peter
 
J

Jim Ley

In the Yahoo! UI event.js file I see the following quite a bit

for (var i=0,len=unloadListeners.length; i<len; ++i) {

when I always just write

for (var i=0; i<unloadListeners.length; ++i) {

Is it worth it to declare the variable len to save time evaluating
unloadListeners.length?

Yes it is.

Jim.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Thu, 31 Aug 2006 16:49:30 remote, seen in
news:comp.lang.javascript, (e-mail address removed) posted :
In the Yahoo! UI event.js file I see the following quite a bit

for (var i=0,len=unloadListeners.length; i<len; ++i) {

when I always just write

for (var i=0; i<unloadListeners.length; ++i) {

Is it worth it to declare the variable len to save time evaluating
unloadListeners.length?

It will save time. But, unless the loop body is small and will be
executed many times, the saving will be small in proportion to the
total.

If the operations in the loop can be performed in the reverse order,

i = unLoadListeners.length ; while (i--) { loop body }

should be a little faster still.

It is better not to use "i" as an identifier in code that others may
read; in some fonts it looks like I j l 1, and may be resented by
grammar-checkers.


You should rarely need to ask questions about the comparative speeds of
equivalent pieces of code, since it is so easy to do the test yourself.

It's a good idea to read the newsgroup and its FAQ.
 
M

Matt Kruse

Dr said:
i = unLoadListeners.length ; while (i--) { loop body }
It is better not to use "i" as an identifier in code that others may
read;

It is a convention across many languages to use i while iterating over
arrays, etc.

Straying from that convention may cause more confusion than someone who
can't tell letters apart...
 
L

Laurent Bugnion

Hi,

Matt said:
It is a convention across many languages to use i while iterating over
arrays, etc.

Straying from that convention may cause more confusion than someone who
can't tell letters apart...

Which languages??? In all the languages I use, the convention is rather
to avoid using one letter variables. In fact, it's generally recommended
to avoid using abbreviations at all. They make the code harder to read
and to maintain.

HTH,
Laurent
 
L

Lee

Laurent Bugnion said:
Hi,



Which languages??? In all the languages I use, the convention is rather
to avoid using one letter variables. In fact, it's generally recommended
to avoid using abbreviations at all. They make the code harder to read
and to maintain.

The "i" index variable is not an abreviation. It is a standard that
goes back at least to FORTRAN, where it was the first of the default
Integer variables.


--
 
L

Laurent Bugnion

Hi,
The "i" index variable is not an abreviation. It is a standard that
goes back at least to FORTRAN, where it was the first of the default
Integer variables.

Still, using one letter names for variables is really not recommended or
recommendable.

HTH,
Laurent
 
E

Evertjan.

Laurent Bugnion wrote on 03 sep 2006 in comp.lang.javascript:
Still, using one letter names for variables is really not recommended or
recommendable.

A meaning, not a fact.

My personal feeling is, that one letter variables are ideal for code parts
that are close together, as often is with a loop or a small function.

function product(x,y){
return x*y;
};

much better than:

function product(firstVariable,secondeOne){
return firstVariable * secondeOne;
};

In modular programming this is often the case.
 
L

Laurent Bugnion

Hi,

Evertjan. said:
Laurent Bugnion wrote on 03 sep 2006 in comp.lang.javascript:


A meaning, not a fact.

I guess you mean "an opinion". You're right. I actually meant "not
recommendable" only. Guidelines are, in the end, matter to interpretations.
My personal feeling is, that one letter variables are ideal for code parts
that are close together, as often is with a loop or a small function.

function product(x,y){
return x*y;
};

much better than:

function product(firstVariable,secondeOne){
return firstVariable * secondeOne;
};

In modular programming this is often the case.

I won't lie, I use one-letter variables occasionally. I help to write
guidelines (one of many activities) in my job as software engineer, for
example the C# guidelines and the JavaScript guidelines that we used in
our last project (a Web application). And yet I sometimes consciously
write code that hurts the guidelines. It's OK as long as you do it
consciously and as long as you document it. However, using one letter
variables just because they were used in fortran doesn't sound like a
good reason to me. Programming languages change, and guidelines change
with them too. The example which comes in mind is Microsoft using polish
notation before, and recommending against it now. I continue to use
polish notation (I have many reasons which make me prefer it) in my
private projects, but I don't use it at work.

Anyway... One could talk about guidelines for hours. For now, I should
rather hit the sack ;-)

Lauren
 
P

petermichaux

Dr said:
You should rarely need to ask questions about the comparative speeds of
equivalent pieces of code, since it is so easy to do the test yourself.

What is the best way to benchmark speeds?

Thanks,
Peter
 
L

Lee

Laurent Bugnion said:
However, using one letter
variables just because they were used in fortran doesn't sound like a
good reason to me.

In my part of the world, putting words into people's mouths is
considered to be quite rude. I didn't suggest the fact that
the index variable "i" was used in FORTRAN as a reason to use
it. I was responding to your claim that all of the languages
you use somehow discourage the use of single character names.

In fact, every programming language manual that I can lay hands
on, from FORTRAN to Java, includes sample code that makes use
of single character loop indices (usually "i") in cases where
the index has no other meaning.


--
 
E

Evertjan.

wrote on 03 sep 2006 in comp.lang.javascript:
What is the best way to benchmark speeds?

There is no "best way" in programming.
Only taste and result.
Mostly taste for John, and many others, I presume.

However storing time before the action, and comparing it with the time
after it, looping the action if the time is small, will get you there.
 
L

Laurent Bugnion

Hi,
Laurent Bugnion said:




In my part of the world, putting words into people's mouths is
considered to be quite rude. I didn't suggest the fact that
the index variable "i" was used in FORTRAN as a reason to use
it.

You are right. I have somehow amalgamated Matt's and your posts into
one. I certainly didn't intend to be rude. Apologies.
I was responding to your claim that all of the languages
you use somehow discourage the use of single character names.

That's still true. I thankfully never had to use Fortran.
In fact, every programming language manual that I can lay hands
on, from FORTRAN to Java, includes sample code that makes use
of single character loop indices (usually "i") in cases where
the index has no other meaning.

And I am talking about guidelines, not manuals. See my previous post
about that.

Greetings,
Laurent
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 2 Sep 2006
15:22:57 remote, seen in Matt Kruse
It is a convention across many languages to use i while iterating over
arrays, etc.


There is a convention, descending from FORTRAN, to use the letters I to
N to start the names of integer variables, and the other letters for
those of floats - but that's convenient only when the code calls for a
compatible mix of integers and floats.

That is not infrequently modified to have I..N starting the names of
counting variables, with the others as other variables, without regard
to whether the latter have integer or general values.

J K I are often used for counters, and N M L for ranges-of-count - also
only when convenient - with that being the general order of preference.

Short-name variables are appropriate for short-range purposes in code,
where it's easier to remember that J is the current counter and what it
represents than it is to type countOfHungryTabbyCats consistently -
especially in a language where variables do not have to be explicitly
declared.

In primitive times, when the ASR33 ruled as an I/O device, there was no
reason to avoid using I as a variable name; it could not be confused
with i or l or j (non-existent), nor with 1 in the available font; and
programming material was rarely mixed with ordinary text. But nowadays,
a code author generally does not know what fonts the code may later be
presented in - especially if it gets in News or on the Web.

Another reason for preferring not to use I as a variable concerns its
effect on searching files with Ctrl-F or similar; the word may also
occur in HTML text or in Javascript strings or comment either as a
capital letter or a Roman numeral.
 
L

Laurent Bugnion

Hi,

Dr John Stockton wrote:

Short-name variables are appropriate for short-range purposes in code,
where it's easier to remember that J is the current counter and what it
represents than it is to type countOfHungryTabbyCats consistently -
especially in a language where variables do not have to be explicitly
declared.

That's where you got to love Intellisense and other similar
technologies, though unfortunately its use is limited with JavaScript.

Another reason for preferring not to use I as a variable concerns its
effect on searching files with Ctrl-F or similar; the word may also
occur in HTML text or in Javascript strings or comment either as a
capital letter or a Roman numeral.

Absolutely.

<snip>

Laurent
 
J

John G Harris

Laurent Bugnion said:
Hi,

Dr John Stockton wrote:



That's where you got to love Intellisense and other similar
technologies, though unfortunately its use is limited with JavaScript.
<snip>

People are so used to

for (var i = 0; ...

that they recognise it immediately, whereas

for (var index = 0; ...

makes them spend time looking at it twice, and

for (var nMyOuterIndex = 0; nMyOuterIndex < max; ++nMyOuterIndex)
arr[nMyOuterIndex] = "";

makes them scream with outrage.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top