2017-03-27 22 views
0
private void btnTestConcatenations_Click(object sender, EventArgs e) 
{ 
    var testTimer = new Stopwatch(); 
    testTimer.Start(); 
    testTimer.Stop(); 
    var elapsedTime = testTimer.Elapsed; 

    var strTest = string.Empty; 

    for (int loopcount = 0; loopcount < NUMBER_CONCATENATIONS_TO_PERFORM; loopcount++) 
    { 
     strTest += "Adding 20 caracters"; 
    } 

    Application.DoEvents();  

답변

0

스톱워치는 작업 시간을 측정하는 데 사용됩니다. 이 메서드에서 발생하는 유일한 다른 작업은 연결 루프이므로, 원하는 시간을 가정하는 것이 안전할까요?

그렇다면, 당신은 같은 것을 할 것입니다 :

private void btnTestConcatenations_Click(object sender, EventArgs e) 
{ 
    var testTimer = new Stopwatch(); 
    var strTest = string.Empty; 
    var numOperations = NUMBER_CONCATENATIONS_TO_PERFORM; 

    // Start the stopwatch 
    testTimer.Start(); 

    // Do some operation that you want to measure 
    for (int loopcount = 0; loopcount < numOperations; loopcount++) 
    { 
     strTest += "Adding 20 characters"; 
    } 

    // Stop the stopwatch 
    testTimer.Stop(); 
    var elapsedTime = testTimer.Elapsed; 

    // Do something with the stopwatch results 
    MessageBox.Show($"It took {elapsedTime} seconds to do {numOperations} concatenations"); 
}