How to create a Stopwatch in C#?

How can you create a stopwatch in C# and why should you do it? Well in this post, you’ll understand everything you need.


Why should you create a Stopwatch in C#?

Isn’t it just amazing all the things that you can do with code? while I’m writing this post, I’m like moving on my chair because I always get so excited when something is about code ๐Ÿ™‚
Anyways, I digress.

So why should you create a stopwatch in C# to be exact? Well, the use case might vary depending on your scenario but in most cases, it’s simply because you want to measure the time it took for something to execute, whether that could be reading the contents of a file or perhaps sending a post request to a third party API. No matter the case, using a stopwatch is the easiest way to do so.

How to create a Stopwatch

For this example, we’ve got a empty console application in C# that we’re going to use to create a stopwatch in C#.

This screenshot shows the code of the empty console application in C#
I named my project “StopwatchExampleTutorial”

So before we all hyper and start throwing code, we first have to do something pretty important. Why? Well, by the Stopwatch system is located inside of a separate namespace (Or path) so we have to import it first into our project before we can make use of it.

So head over to the top of your code and add the following line of code:

This screenshots shows the line of code you have to add on top of your code file.
The line you have to add.

This will add the namespace to our current code file so we can access everything that’s inside of that specific namespace.

Adding the stopwatch to our application

Now, let’s create the stopwatch inside of your C# application. Here’s how to do that.
Go inside of the “public static void Main” method and write the following piece of code:

This image shows the piece of code that will create a new stopwatch.
The piece of code that will create a new stopwatch.

This will automatically create a new stopwatch and also start it immediately. In the next step, we’ll write a piece of code that we will be measuring the time off.

Writing the code we want to measure the time off.

Now, let’s write the piece of code that we want to measure the time of that it took to execute.

This is  the piece of code that we'll be mearing the time off that it took to execute.
The code we will measure.

Now in the next step, we’ll finish off our application by stopping the stopwatch and printing out the elapsed time it took to execute.

Stopping the stopwatch and checking the elapsed time

Now the following pieces of code will stop our current stopwatch from counting and printing out the elapsed time inside of the console of our application once we run it.

The code that will stop the stopwatch and show the elapsed time.

The next step is to run your application. and it will show something similar to this:

The final result that will be shown inside of the console application.

Congratulations

Congratulations, you just created your very own stopwatch in C# that will measure how long it takes to execute a certain piece of code inside of your C# application.

YouTube Video

Was this to hard to follow or perhaps you prefer watching a video showcasing the exact same steps instead?
Then watch my YouTube about “How To Create a Stopwatch in C#”
HERE

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.