Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the more efficient syntax to produce an even number in my console project?

Tags:

c#

I recently started learning c# and learned that there are 2 ways to have my code output even numbers when I write this For loop. I learned the syntax of version 2, which is intuitive to me. However, version 1 was an exampled I found elsewhere online. I want to understand if there is a difference between the two versions.

//Version 1
for (int num = 0; num < 100; num+=2) 
{
    Console.WriteLine (num); 
}

//Version 2
for (int num = 0; num < 100; num++)
{
    if (num % 2 == 0)
    {
        Console.WriteLine (num);
    } 
}

Of the two possible ways, is there any difference between the two syntaxes? If yes, which is more efficient and why?

like image 946
bmeredit Avatar asked Jul 10 '15 22:07

bmeredit


People also ask

What is the correct syntax of declaring and defining a reference?

The operator '&' is used to declare reference variable. The following is the syntax of reference variable. datatype variable_name; // variable declaration datatype& refer_var = variable_name; // reference variable. Here, datatype − The datatype of variable like int, char, float etc.

What does <= mean in C++?

Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.


1 Answers

For a given value of N (in your sample code, N=100)

  • Version #1 does N/2 additions
  • Version #2 does does N additions, plus N integer divisions (a relatively expensive operation).

And you forgot Version 3, bit-twiddling. Bitwise operations are a whole lot cheaper than division, and since we know that integers in the C# world are two's-complement binary values, the state of the low-order bit tells us whether an integer is even or not, thus:

bool isEven( int n ) { return 0 == ( n & 1 ) ; }

We can write a test harness, like this:

class Program
{
  public static int Version1(int n)
  {
    int cnt = 0;
    for ( int i = 0 ; i < n ; i+=2 )
    {
      ++cnt;
    }
    return cnt;
  }

  public static int Version2(int n)
  {
    int cnt = 0;
    for ( int i = 0 ; i < n ; ++i )
    {
      if ( i % 2 == 0 )
      {
        ++cnt;
      }
    }
    return cnt;
  }

  public static int Version3(int n)
  {
    int cnt = 0;
    for ( int i = 0 ; i < n ; ++i )
    {
      if ( 0 == (i & 1) )
      {
        ++cnt;
      }
    }
    return cnt;
  }

  private static void Main(string[] args)
  {
    int n = 500000000;
    Stopwatch timer = new Stopwatch();

    timer.Start();
    Version1( n );
    timer.Stop();
    Console.WriteLine( "{0:c} -- Version #1 (incrementing by 2)" , timer.Elapsed ) ;

    timer.Restart();
    Version2(n);
    timer.Stop();
    Console.WriteLine( "{0:c} -- Version #2 (incrementing by 1 with modulo test)" , timer.Elapsed ) ;

    timer.Restart();
    Version3(n);
    timer.Stop();
    Console.WriteLine( "{0:c} -- Version #3 (incrementing by 1 with bit twiddling)" , timer.Elapsed ) ;

    return;

  }

}

And find out which is actually faster. The above program runs the loop 500,000,000 times so we get numbers that are big enough to measure.

Here are the timings I get with VS 2013:

  • Debug build (non-optimized):

    00:00:00.5500486 -- Version #1 (incrementing by 2)
    00:00:02.0843776 -- Version #2 (incrementing by 1 with modulo test)
    00:00:01.2507272 -- Version #3 (incrementing by 1 with bit twiddling)
    
    • Version #2 is 3.789 times slower than version #1
    • Version #3 is 2.274 times slower than version #1
  • Release build (optimized)

    00:00:00.1680107 -- Version #1 (incrementing by 2)
    00:00:00.5109271 -- Version #2 (incrementing by 1 with modulo test)
    00:00:00.3367961 -- Version #3 (incrementing by 1 with bit twiddling)
    
    • Version #2 is 3.041 times slower than version #1
    • Version #3 is 2.005 times slower than version #1
like image 165
Nicholas Carey Avatar answered Sep 20 '22 12:09

Nicholas Carey