Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to calculate the code execution time in C#

2 Answers

0 votes
using System;

class Program
{
    static void Main() {
        var watch = System.Diagnostics.Stopwatch.StartNew();

        long sum = 0;       
        for (long i = 0; i < 9387391; i++) {
            sum += i;
        }
        
        watch.Stop();
        
        Console.WriteLine(sum);
        
        Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
    }
}




/*
run:
  
44061550199745
Execution Time: 9 ms
  
*/

 



answered Aug 12, 2021 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        var watch = System.Diagnostics.Stopwatch.StartNew();

        long sum = 0;       
        for (long i = 0; i < 9387391; i++) {
            sum += i;
        }
        
        watch.Stop();
        
        Console.WriteLine(sum);
        
        Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
        
        if (!watch.IsRunning)
            watch.Restart(); 
            
        sum = 0;
        for (long i = 0; i < 9000292008; i++) {
            sum += i;
        }
        
        watch.Stop();
        
        Console.WriteLine(sum);
        
        Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
    }
}




/*
run:
  
44061550199745
Execution Time: 23 ms
3609139962715086796
Execution Time: 9271 ms
  
*/

 



answered Aug 12, 2021 by avibootz

Related questions

1 answer 136 views
136 views asked Sep 26, 2021 by avibootz
3 answers 198 views
1 answer 292 views
2 answers 239 views
2 answers 256 views
1 answer 227 views
1 answer 203 views
...