Skip to content

Instantly share code, notes, and snippets.

@kamyker
Last active November 9, 2023 12:06
Show Gist options
  • Save kamyker/851696cfed9c2cd725b496e2ef2d1900 to your computer and use it in GitHub Desktop.
Save kamyker/851696cfed9c2cd725b496e2ef2d1900 to your computer and use it in GitHub Desktop.
Unity Mono vs Il2cpp vs NET 6 vs NativeAOT (CoreRT)
using System;
using System.Diagnostics;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// stopwatch.Restart();
//benchmark.Run();
// time = stopwatch.ElapsedTicks;
//Benchmarks.MainFake();
//var watch = Stopwatch.StartNew();
//BenchGC.RunClasses();
//watch.Stop();
//Console.WriteLine(watch.ElapsedTicks);
GC.Collect();
var watch = Stopwatch.StartNew();
BenchGC.RunStrings();
watch.Stop();
Console.WriteLine(watch.ElapsedTicks);
GC.Collect();
watch = Stopwatch.StartNew();
BenchGC.RunClasses();
watch.Stop();
Console.WriteLine(watch.ElapsedTicks);
GC.Collect();
watch = Stopwatch.StartNew();
BenchGC.RunArrays();
watch.Stop();
Console.WriteLine(watch.ElapsedTicks);
Console.ReadLine();
}
}
class BenchGC
{
public static int RunStrings()
{
string text = "Hello";
int a = 0;
for(int i = 0; i < 1_000_000_000; i++)
{
text = text.Substring(0, text.Length - 1);
a += text.Length;
text += "a";
}
return text.Length;
}
public static float RunClasses()
{
ClassTest test;
float a= 0;
for(int j = 0; j < 5; j++)
{
for(int i = 0; i < 1_000_000_000; i++)
{
test = new ClassTest();
test.Test1 = i;
a += test.Test1;
}
}
return a;
}
public static float RunStructs()
{
StructTest test;
float a= 0;
for(int i = 0; i < 1_000_000_000; i++)
{
test = new StructTest();
test.Test1 = i;
a += test.Test1;
}
return a;
}
public static int RunArrays()
{
int[] test;
int a = 0;
for(int i = 0; i < 300_000; i++)
{
test = new int[i];
a += test.Length;
}
return a;
}
}
class ClassTest
{
public float Test1;
public float Test2;
public float Test3;
public float Test4;
public float Test5;
public float Test6;
public float Test7;
public float Test8;
public float Test9;
public float Test10;
}
struct StructTest
{
public float Test1;
public float Test2;
public float Test3;
public float Test4;
public float Test5;
public float Test6;
public float Test7;
public float Test8;
public float Test9;
public float Test10;
}
}
@kamyker
Copy link
Author

kamyker commented Aug 7, 2021

Result:
https://cdn.discordapp.com/attachments/198816752170303488/873647823999107182/unknown.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment