我的代码是否完全运行? [英] Does my code run exactly?

查看:78
本文介绍了我的代码是否完全运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是真的,因为它太快了。

请检查帮帮我!



 使用系统; 
使用 System.Collections.Generic;
使用 System.Diagnostics;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 ConsoleApplication1
{
class 计划
{
静态 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 ++) // 运行10毫秒
{
int c =(a + b);
i ++;
}
sw.Stop();
// __________________________________________________________________

Console.WriteLine( 纳秒时间为:{0}(ns),nano.ToString());
Console.ReadLine();
}
}
}





我收到了一些结果:



纳秒时间  2400 (ns)





另一次,它是2500,27

只有2k纳秒,持续10百万次?



这是我的硬件:

英特尔酷睿i5 4200M

内存:2Gb

操作系统:Windows 8.1

解决方案

很难说 - 但这不是10,000,000次迭代:它只有5,000,000,因为你在每个循环中增加 i 两次。



问题是循环的大部分是一个固定计算,所以任何一半体面的优化器都会愉快地取出循环内容(并且可以很容易地处理循环本身)。但是......如果你在调试器中运行它,那么优化几乎被禁用,所以不应该删除它。



不,它不是纳秒秒数...你打开一个你在循环开始之前计算的值!,秒表类不准确到纳秒!

试试这个:

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

// ______________________________________________________________
秒表sw = Stopwatch.StartNew( );
for int i = 0 ; i < 10000000 ; i ++) // 运行10毫秒
{
int c =(a + b);
}
sw.Stop();
// __________________________________________________________________

Console.WriteLine( 毫秒时间为:{0} ms,sw.ElapsedMilliseconds);
Console.ReadLine();


结果不正确。你必须移动以下行

Quote:

nano + = sw.Elapsed.TotalMilliseconds * 1000000; sw.Stop(); 语句之后的块引用>


I'm not really it's exactly because it's so quickly.
Please check help me!

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:

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

解决方案

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:

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();


The result is incorrect. You have to move the following line

Quote:

nano += sw.Elapsed.TotalMilliseconds * 1000000;


after the sw.Stop(); statement.


这篇关于我的代码是否完全运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆