Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm not really it's exactly because it's so quickly.
Please check help me!

C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            var sw = Stopwatch.StartNew();
            double nano = 0.00;
            nano += sw.Elapsed.TotalMilliseconds * 1000000;

            int a, b;
            a = 1; b = 2;

            // ______________________________________________________________
            sw.Start();

            for (int i = 0; i < 10000000; i++) // Run 10 milliions times
            {
                int c = (a + b);
                i++;
            }
            sw.Stop();
            // __________________________________________________________________

            Console.WriteLine("Nanoseconds time is: {0} (ns)", nano.ToString());
            Console.ReadLine();
        }
    }
}


And some results i received:

C#
Nanoseconds time is: 2400 (ns)


Another time, it's 2500, 2700
Only 2k nanoseconds for 10 millions times?

And here is my hardware:
Intel core i5 4200M
RAM: 2Gb
OS: Windows 8.1
Debugger: Visual Studio Debugger
Posted
Comments
ZurdoDev 8-Sep-14 9:31am    
What?
Nathan Minier 8-Sep-14 9:39am    
I'd also like to throw in that because of register size vs the size of the operations you're doing, you can do 2 iterations per cycle per thread, and your chipset is 2.5gHz stock, which is 2.5 billion operations per second. Just saying.
user8x86 8-Sep-14 9:51am    
Thanks for the advance

It's difficult to tell - but that isn't 10,000,000 iterations: it's only 5,000,000 because you increment i twice in each loop.

The problem is that the bulk of the loop is a "fixed calculation" so any half decent optimiser will happily take out the loop content (and could easily dispose of the loop itself as well). But...if you are running this in the debugger, then optimisations are pretty much disabled, so it shouldn't be removed.

And no, it's not the number of nano seconds... You print a value you calculate before the loop is even started!, and the Stopwatch class isn't accurate to nanoseconds anyway!
Try this:
C#
int a, b;
a = 1; b = 2;

// ______________________________________________________________
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++) // Run 10 milliions times
    {
    int c = (a + b);
    }
sw.Stop();
// __________________________________________________________________

Console.WriteLine("Milliseconds time is: {0}ms", sw.ElapsedMilliseconds);
Console.ReadLine();
 
Share this answer
 
The result is incorrect. You have to move the following line
Quote:
nano += sw.Elapsed.TotalMilliseconds * 1000000;

after the sw.Stop(); statement.
 
Share this answer
 
Comments
user8x86 8-Sep-14 9:49am    
Thank Mr.CPallini. That worked.
CPallini 8-Sep-14 10:53am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900