if与赋值语句的效率 [英] Efficiency of if versus assignment statements

查看:137
本文介绍了if与赋值语句的效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我有一个效率问题.

具有单一条件的if语句是否比赋值语句占用更多的cpu时间?
Ex,效率更高:

if(objA!= objB)
objA = objB;

或just

objA = objB;

Hey guys, I have an efficiency question.

Does an if statement with a single condition take up more cpu time than an assignment statement?
Ex, which is more efficient:

if (objA != objB)
objA = objB;

or just

objA = objB;

推荐答案

Fudge Mutator写道:
Fudge Mutator wrote:

具有单个条件的if语句是否比赋值语句占用更多的cpu时间?

Does an if statement with a single condition take up more cpu time than an assignment statement?



几乎可以肯定.

除非您的分配非常复杂(例如大对象的深层副本),否则进行比较的时间将超过每次进行分配的时间,无论如何.

假设您要使两个字符串相等,当且仅当它们不相同时.因此,您首先要像这样比较它们:



Almost certainly.

Unless your assignment is very complex (like a deep, deep copy of a large object), the time to do the comparison will outweigh just doing the assignment each time, regardless.

Let''s say you want to make two strings equal, if and only if they are not the same. So you compare them first like this:

static void CheckAndMakeEqual(ref string myString1, ref string myString2)
{
    if (myString1 != myString2)
        myString1 = myString2;
}

...如果字符串已经相等,您可能会认为通过避免分配来节省时间.

但是即使您在绝大多数时间里都知道字符串相等,下面的代码也会更快 :

...you might think you are saving time by avoiding the assignment if the strings are already equal.

But the code below will be much faster even if you know the strings are equal the vast, vast majority of the time:

static void JustMakeEqual(ref string myString1, ref string myString2)
 {
         myString1 = myString2;
 }

...比快四倍以上,在这种情况下:

...more than four times faster, in this case:

After 100,000,000 iterations,<br />
CheckAndMakeEqual() ran in 0.4808 seconds;<br />
JustMakeEqual() ran in 0.1180 seconds.




如果您想自己尝试一下,这是时间代码.请注意,字符串始终相等(即分配从未发生),并且每次都只执行分配要快得多.

代码在这里:




Here is the timing code, if you want to try this out for yourself. Notice that the strings are always equal (i.e. the assignment never happens) and still just doing the assignment each time is much faster.

Code Here:

using System;
using System.Diagnostics;

class Program
{
    static void CheckAndMakeEqual(ref string myString1, ref string myString2)
    {
        if (myString1 != myString2)
            myString1 = myString2;
    }

    static void JustMakeEqual(ref string myString1, ref string myString2)
    {
            myString1 = myString2;
    }

    static void Main(string[] args)
    {
        const int RepeatCount = 100000000;
        string myString1 = "This";  // Two strings, already equal
        string myString2 = "This";

        // Start the timer.
        Console.WriteLine("Testing CheckAndMakeEqual() {0} times. Timer started...", RepeatCount);
        Stopwatch timer = Stopwatch.StartNew();

        // Run the test many times.
        for (int i = 0; i < RepeatCount; i++)
        {
            CheckAndMakeEqual(ref myString1, ref myString2);   // Test the first method.
        }

        // Stop the timer.
        timer.Stop();
        Console.WriteLine("Timer stopped. Execution Time: {0} seconds.", timer.Elapsed.ToString());

        // Start the timer.
        Console.WriteLine("\nTesting JustMakeEqual() {0} times. Timer started...", RepeatCount);
        timer = Stopwatch.StartNew();

        // Run the test many times.
        for (int i = 0; i < RepeatCount; i++)
        {
            JustMakeEqual(ref myString1, ref myString2);       // Test the second method.
        }

        // Stop the timer.
        timer.Stop();
        Console.WriteLine("Timer stopped. Execution Time: {0} seconds.", timer.Elapsed.ToString());
    }
}



享受,

Robert C. Cartaino



Enjoy,

Robert C. Cartaino

于2008年7月22日,星期二,下午2:55修改
modified on Tuesday, July 22, 2008 2:55 PM


这篇关于if与赋值语句的效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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