Skip to content

Instantly share code, notes, and snippets.

@utterstep
Created March 28, 2013 08:11
Show Gist options
  • Save utterstep/5261540 to your computer and use it in GitHub Desktop.
Save utterstep/5261540 to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
namespace Tests
{
class Program
{
static void Main(string[] args)
{
Benchmark(int.MaxValue / 2);
}
private static void Benchmark(int iterations)
{
#region Вывод информации о платформе
PortableExecutableKinds peKind;
ImageFileMachine fMachine;
var curentAssembly = Assembly.GetExecutingAssembly();
curentAssembly.GetModules()[0].GetPEKind(out peKind, out fMachine);
Console.WriteLine(peKind.HasFlag(PortableExecutableKinds.PE32Plus) ? "Running 64-bit assembly" : "Running 32-bit assembly");
#endregion
System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
long temp = 0; //измени тип этой переменной для проведения тестов необходимого типа
int ITER_COUNT = iterations;
Console.WriteLine("Now we're testing {0} type.\nNumber of iterations: {1}", temp.GetType(), ITER_COUNT);
Console.WriteLine();
#region Умножение и деление
temp = 1;
s.Restart();
for (int i = 0; i < ITER_COUNT; i++)
{
temp *= 3;
}
s.Stop();
Console.WriteLine("Multiptlication: {0} ms", s.ElapsedMilliseconds);
temp = int.MaxValue;
s.Restart();
for (int i = 0; i < ITER_COUNT; i++)
{
temp /= 2;
}
s.Stop();
Console.WriteLine("Division: {0} ms", s.ElapsedMilliseconds);
#endregion
#region Сдвиги
temp = 1;
s.Restart();
for (int i = 0; i < ITER_COUNT; i++)
{
temp = (temp << 1) + (temp << 0); //умножение на произвольное число можнно записать в виде сдвигов, при необходимости
//но, польза от этого будет только при работе с типами,
//не вмещающимися в один регистр процессора
}
s.Stop();
Console.WriteLine("Left shift: {0} ms", s.ElapsedMilliseconds);
temp = int.MaxValue;
s.Restart();
for (int i = 0; i < ITER_COUNT; i++)
{
temp >>= 1;
}
s.Stop();
Console.WriteLine("Right shift: {0} ms", s.ElapsedMilliseconds);
Console.WriteLine();
Console.WriteLine("FINISH");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment