Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #44 from lewurm/isinst-bench
Browse files Browse the repository at this point in the history
benchmarks: microbenchmark for `isinst` and `castclass`
  • Loading branch information
schani committed Apr 14, 2016
2 parents 920abb1 + 9f51bec commit 02be72e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions benchmarks/isinst.benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Name": "isinst",
"TestDirectory": "small",
"CommandLine": ["isinst.exe", "25000000"]
}
51 changes: 51 additions & 0 deletions tests/small/isinst.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;

public class MyTypes {
public class A {
public virtual long foo () {
return 1;
}
}

public class B : A {
public override long foo () {
return 2;
}
}

public class C : A {
public override long foo () {
return 3;
}
}

static bool bench (long iterations, object[] objs) {
long checksum = 0;
for (long i = 0; i < iterations; ++i) {
foreach (object o in objs) {
if (o is A) {
checksum += ((A) o).foo ();
}
if (o is B) {
checksum += ((B) o).foo ();
}
if (o is C) {
checksum += ((C) o).foo ();
}
}
}
return checksum == (4 * iterations);
}

public static void Main (string []args) {
long n = 25000000L;
object[] objs = new object[4];
objs [0] = new object ();
objs [1] = new A ();
objs [2] = new B ();
objs [3] = new C ();
if (args.Length > 0)
n = Int64.Parse (args[0]);
System.Environment.Exit (bench (n, objs) ? 0 : 1);
}
}
Binary file added tests/small/isinst.exe
Binary file not shown.

0 comments on commit 02be72e

Please sign in to comment.