Re: Another question about performance
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sat, 24 Dec 2005 07:35:48 -0000
Michael Nemtsev <nemtsev@xxxxxxx> wrote:
> Hello Jon Skeet [C# MVP],
>
> J> Michael Nemtsev <nemtsev@xxxxxxx> wrote:
> J>
> >> But it should be taken into account. In my work I regularly meet the
> >> conditions when for example delay for 2 secons brakes functional
> >> requirements.
> >>
> J> It would be a very rare situation where using a different form of
> J> iteration would cost you even *nearly* that much.
>
> Indeed. Just a simple iterating 10,000,000 of int in int[] give u such
> difference
> with for and foreach
Hmm. Not on my box - iterating through 10,000,000 ints using foreach
takes 0.03 seconds. Here's my test code:
using System;
class Test
{
public static void Main(String [] args)
{
int[] x = new int[10000000];
int total=0;
DateTime start = DateTime.Now;
foreach (int y in x)
{
total += y;
}
Console.WriteLine (DateTime.Now-start);
}
}
What does that print on your box?
Don't forget to run it outside a debugger.
> J> Now, are you really doing that much iteration in the space of time in
> which two seconds breaks functional
> J> requirements?
>
> yep, but not just only using for :)
> >> Itetating for 1,000,000 recors with for in 3-4 times faster than
> >> foreach.
> >>
> J> With what type? An ArrayList or an array? Big difference.
>
> let's take just int[] (not talking abt datasets ;) ), performance in
> that case differs in 2 times
Not in my experiments. Here's another example:
using System;
class Test
{
public static void Main(String [] args)
{
int[] x = new int[10000000];
int total=0;
DateTime start = DateTime.Now;
for (int iteration = 0; iteration < 100; iteration++)
{
foreach (int y in x)
{
total += y;
}
}
Console.WriteLine (DateTime.Now-start);
start = DateTime.Now;
for (int iteration = 0; iteration < 100; iteration++)
{
for (int i=0; i < x.Length; i++)
{
total += x[i];
}
}
Console.WriteLine (DateTime.Now-start);
}
}
On my box, that prints:
00:00:02.1093750
00:00:02.0625000
In other words, not only can it do 100 times as many iterations as you
claim to do before even reaching 2 seconds, but the speed difference is
pretty insignificant. Now, the difference is more significant if you
comment out the addition, but I think it's reasonable to assume you
want to do *something* with the value inside the loop...
I don't understand how you're getting such different results, unless
you're running them in the debugger. Even in the debugger I'm not
seeing as much of a difference as you seem to be though.
What code did you use to test the difference?
> J> And what are you doing *inside* the loop? If you're not doing
> J> anything significant, you should ask yourself how often you actually
> J> iterate through a million records to do nothing with them. More
> J> often, in my experience, you do something significant within the loop
> J> which makes the actual iteration cost irrelevant.
>
> Agree, but using *for* instead *foreach* in code not so hard (when even
> code navigation tools generate *for* template automatically) to win a bit
> more performance, if u r even only iterating and perform some financial calculations.
>
> Jon, I'm not trying to argue abt *foreach* is bad and *for* rocks, I just
> wanna try to say, that using *for* is too simple .
> It's not a panacea. But if u r developing back-end/server side app, using
> *for* is more proper way
Only when you've found you've *actually* got a problem. It's easier to
make a mistake with for than it is with foreach, and if you use foreach
it's clear from reading the code that you're *only* interested in the
values, and not in their positions. In other words, the code is simpler
and conveys more correct information. *That* sounds like a the "more
proper" way unless you *know* that you actually gain something
significant from using a for loop.
Using the test code I've posted above, you'd have to be iterating
through an array of 10,000,000 ints 5000 times in order to get a two
second performance improvement. Now, unless you're actually doing very,
very little work inside the loop, chances are that going through that
much data is going to take so much time that 2 seconds becomes
irrelevant.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- Follow-Ups:
- Re: Another question about performance
- From: Michael Nemtsev
- Re: Another question about performance
- References:
- Re: Another question about performance
- From: Jon Skeet [C# MVP]
- Re: Another question about performance
- From: Michael Nemtsev
- Re: Another question about performance
- Prev by Date: Re: C# server side (encode) -> C++ client side (decode) ?
- Next by Date: Re: Random Access Files
- Previous by thread: Re: Another question about performance
- Next by thread: Re: Another question about performance
- Index(es):
Relevant Pages
|