Click here to Skip to main content
15,887,485 members
Home / Discussions / C#
   

C#

 
AnswerRe: Display the information in the gridview for current user asp.net C# Pin
half-life22-Jul-08 7:42
half-life22-Jul-08 7:42 
Questionreturns 0 Pin
netJP12L22-Jul-08 7:33
netJP12L22-Jul-08 7:33 
AnswerRe: returns 0 Pin
half-life22-Jul-08 7:44
half-life22-Jul-08 7:44 
Questionmonth calender Pin
Mahdi_kishislan22-Jul-08 7:17
Mahdi_kishislan22-Jul-08 7:17 
AnswerRe: month calender Pin
half-life22-Jul-08 7:36
half-life22-Jul-08 7:36 
QuestionEfficiency of if versus assignment statements Pin
Fudge Mutator22-Jul-08 6:50
Fudge Mutator22-Jul-08 6:50 
AnswerRe: Efficiency of if versus assignment statements Pin
half-life22-Jul-08 7:39
half-life22-Jul-08 7:39 
AnswerRe: Efficiency of if versus assignment statements [modified] PinPopular
Robert.C.Cartaino22-Jul-08 8:03
Robert.C.Cartaino22-Jul-08 8:03 
Fudge Mutator wrote:
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());
    }
}


Enjoy,

Robert C. Cartaino

modified on Tuesday, July 22, 2008 2:55 PM

GeneralRe: Efficiency of if versus assignment statements Pin
Fudge Mutator22-Jul-08 8:10
Fudge Mutator22-Jul-08 8:10 
GeneralRe: Efficiency of if versus assignment statements Pin
PIEBALDconsult22-Jul-08 8:14
mvePIEBALDconsult22-Jul-08 8:14 
GeneralRe: Efficiency of if versus assignment statements Pin
hammerstein0522-Jul-08 8:11
hammerstein0522-Jul-08 8:11 
AnswerRe: Efficiency of if versus assignment statements Pin
PIEBALDconsult22-Jul-08 8:10
mvePIEBALDconsult22-Jul-08 8:10 
AnswerRe: Efficiency of if versus assignment statements [Modified] Pin
Luc Pattyn22-Jul-08 8:15
sitebuilderLuc Pattyn22-Jul-08 8:15 
QuestionApplication Deployment Pin
BlitzPackage22-Jul-08 6:43
BlitzPackage22-Jul-08 6:43 
AnswerRe: Application Deployment Pin
Garrett Pauls22-Jul-08 7:02
Garrett Pauls22-Jul-08 7:02 
GeneralRe: Application Deployment Pin
BlitzPackage22-Jul-08 8:05
BlitzPackage22-Jul-08 8:05 
GeneralRe: Application Deployment Pin
Garrett Pauls22-Jul-08 8:25
Garrett Pauls22-Jul-08 8:25 
GeneralRe: Application Deployment Pin
BlitzPackage22-Jul-08 8:54
BlitzPackage22-Jul-08 8:54 
GeneralRe: Application Deployment Pin
selcuks24-Jul-08 21:04
selcuks24-Jul-08 21:04 
QuestionClass Cleanup Challenge Pin
lisad_tgc22-Jul-08 5:53
lisad_tgc22-Jul-08 5:53 
AnswerRe: Class Cleanup Challenge Pin
Anthony Mushrow22-Jul-08 5:56
professionalAnthony Mushrow22-Jul-08 5:56 
QuestionMultithreading and XML DOM Pin
Justin Time22-Jul-08 5:14
Justin Time22-Jul-08 5:14 
QuestionMouseMove event not firing for newly added control [modified] Pin
Michael Dausmann22-Jul-08 4:59
Michael Dausmann22-Jul-08 4:59 
AnswerRe: MouseMove event not firing for newly added control Pin
half-life22-Jul-08 7:50
half-life22-Jul-08 7:50 
GeneralRe: MouseMove event not firing for newly added control Pin
Michael Dausmann22-Jul-08 12:15
Michael Dausmann22-Jul-08 12:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.