Skip to content

Instantly share code, notes, and snippets.

@duckymirror
Last active February 3, 2019 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duckymirror/1690cb086cef46521676ff06bdfb3b7d to your computer and use it in GitHub Desktop.
Save duckymirror/1690cb086cef46521676ff06bdfb3b7d to your computer and use it in GitHub Desktop.
This measures the time for conversions of integers to strings and logging them in the console.
using System;
using System.Diagnostics;
namespace WriteLineSpeed
{
class Program
{
static void Main(string[] args)
{
Stopwatch stopwatch = new Stopwatch();
Console.WriteLine("Conversion tests");
Console.WriteLine("i + \"\"");
stopwatch.Start();
for (int i = 0; i < 1000000; i++)
{
string s = i + "";
}
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.WriteLine("i.ToString()");
stopwatch.Restart();
for (int i = 0; i < 1000000; i++)
{
string s = i.ToString();
}
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.WriteLine("Console.WriteLine tests");
Console.WriteLine("i+\"\"");
stopwatch.Restart();
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i + "");
}
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.WriteLine("---------------------------------");
Console.WriteLine("i");
stopwatch.Restart();
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i);
}
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment