Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- class Base {
- public virtual void f(int size, string name, bool flag)
- {
- Program.Method3(flag: true, size: 1, name: "Perl");
- }
- }
- class Derived : Base {
- public override void f(int size, string name, bool flag)
- {
- Program.Method3(flag: true, size: 1, name: "Perl");
- }
- }
- class Program
- {
- const int _max = 100000000;
- static void Main()
- {
- var b = new Derived();
- Method1(b);
- Method2(b);
- var s0 = Stopwatch.StartNew();
- for (int i = 0; i < _max; i++)
- {
- Method1(b);
- }
- for (int i = 0; i < _max; i++)
- {
- Method2(b);
- }
- s0.Stop();
- // begin real test
- var s1 = Stopwatch.StartNew();
- for (int i = 0; i < _max; i++)
- {
- Method1(b);
- }
- s1.Stop();
- var s2 = Stopwatch.StartNew();
- for (int i = 0; i < _max; i++)
- {
- Method2(b);
- }
- s2.Stop();
- // begin real test reversed order
- var s4 = Stopwatch.StartNew();
- for (int i = 0; i < _max; i++)
- {
- Method2(b);
- }
- s4.Stop();
- var s3 = Stopwatch.StartNew();
- for (int i = 0; i < _max; i++)
- {
- Method1(b);
- }
- s3.Stop();
- Console.WriteLine("1 named: {0}", ((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) /
- _max).ToString("0.00 ns"));
- Console.WriteLine("1 raw : {0}", ((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) /
- _max).ToString("0.00 ns"));
- Console.WriteLine("2 named: {0}", ((double)(s3.Elapsed.TotalMilliseconds * 1000 * 1000) /
- _max).ToString("0.00 ns"));
- Console.WriteLine("2 raw : {0}", ((double)(s4.Elapsed.TotalMilliseconds * 1000 * 1000) /
- _max).ToString("0.00 ns"));
- Console.Read();
- }
- static void Method1(Base b)
- {
- // Method3(flag: true, size: 1, name: "Perl");
- b.f(flag: true, size: 1, name: "Perl");
- }
- static void Method2(Base b)
- {
- // Method3(1, "Perl", true);
- b.f(1, "Perl", true);
- }
- public static void Method3(int size, string name, bool flag)
- {
- if (!flag && size != -1 && name != null)
- {
- throw new Exception();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement