Created
November 19, 2018 12:05
-
-
Save Ivony/98281c152f732dcf7d691c588f3734e9 to your computer and use it in GitHub Desktop.
C#_Java_performance_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
public interface I | |
{ | |
void Do(); | |
} | |
public class C : I | |
{ | |
private long number; | |
public void Do() | |
{ | |
number++; | |
} | |
} | |
public struct S : I | |
{ | |
private long number; | |
public void Do() | |
{ | |
number++; | |
} | |
} | |
static void Main( string[] args ) | |
{ | |
var watch = new Stopwatch(); | |
var instance = (I) new C(); | |
watch.Start(); | |
for ( long i = 0; i < 10_0000_0000; i++ ) | |
{ | |
instance.Do(); | |
} | |
watch.Stop(); | |
Console.WriteLine( $"Test I(C): {watch.ElapsedMilliseconds}" ); | |
instance = (I) new S(); | |
watch.Restart(); | |
for ( long i = 0; i < 10_0000_0000; i++ ) | |
{ | |
instance.Do(); | |
} | |
watch.Stop(); | |
Console.WriteLine( $"Test I(S): {watch.ElapsedMilliseconds}" ); | |
var c = new C(); | |
watch.Restart(); | |
for ( long i = 0; i < 10_0000_0000; i++ ) | |
{ | |
c.Do(); | |
} | |
watch.Stop(); | |
Console.WriteLine( $"Test C: {watch.ElapsedMilliseconds}" ); | |
var s = new S(); | |
watch.Restart(); | |
for ( long i = 0; i < 10_0000_0000; i++ ) | |
{ | |
s.Do(); | |
} | |
watch.Stop(); | |
Console.WriteLine( $"Test S: {watch.ElapsedMilliseconds}" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment