Opentk/Source/Examples/Tests/S01_Call_Performance.cs

173 lines
6.1 KiB
C#
Raw Normal View History

#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
2007-08-01 09:32:49 +00:00
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
2007-08-01 09:32:49 +00:00
using System.Windows.Forms;
using OpenTK.OpenGL;
using System.Security;
2007-08-01 09:32:49 +00:00
namespace Examples.Tests
{
public class S01_Call_Performance : IExample
2007-08-01 09:32:49 +00:00
{
OpenTK.GLContext context;
const int num_calls = 1000000;
float[] v = new float[] { 0.0f, 0.0f };
public static int dummy_variable = 0;
public void Launch()
2007-08-01 09:32:49 +00:00
{
using (Form f = new Form())
{
context = new OpenTK.GLContext(new OpenTK.DisplayMode(), new OpenTK.Platform.WindowInfo(f));
context.CreateContext();
2007-08-01 09:32:49 +00:00
Trace.WriteLine(String.Format("Number of calls: {0}", num_calls));
2007-08-01 09:32:49 +00:00
Stopwatch timer = new Stopwatch();
2007-08-01 09:32:49 +00:00
#region Managed functions
2007-08-01 09:32:49 +00:00
Trace.Write("Timing empty loop: ");
timer.Start();
for (int i = 0; ++i < num_calls; )
{
}
timer.Stop();
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
timer.Reset();
2007-08-01 09:32:49 +00:00
Trace.Write("Timing inline .Net functions: ");
timer.Start();
for (int i = 0; ++i < num_calls; )
{
InlineFunction();
}
timer.Stop();
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
timer.Reset();
2007-08-01 09:32:49 +00:00
Trace.Write("Timing virtual .Net functions: ");
timer.Start();
for (int i = 0; ++i < num_calls; )
2007-08-01 09:32:49 +00:00
{
VirtualFunction();
2007-08-01 09:32:49 +00:00
}
timer.Stop();
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
timer.Reset();
2007-08-01 09:32:49 +00:00
#endregion
2007-08-01 09:32:49 +00:00
#region OpenTK.OpenGL
2007-08-01 09:32:49 +00:00
Trace.Write("Timing OpenTK.OpenGL core functions: ");
timer.Start();
for (int i = 0; ++i < num_calls; )
{
GL.Vertex2(0.0f, 0.0f);
}
timer.Stop();
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
timer.Reset();
2007-08-01 09:32:49 +00:00
Trace.Write("Timing OpenTK.OpenGL core functions (array): ");
timer.Start();
for (int i = 0; ++i < num_calls; )
{
GL.Vertex2(v);
}
timer.Stop();
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
timer.Reset();
2007-08-01 09:32:49 +00:00
Trace.Write("Timing OpenTK.OpenGL core functions (void*): ");
timer.Start();
for (int i = 0; ++i < num_calls; )
{
GL.CallLists(v.Length, GL.Enums.ListNameType.FLOAT, v);
2007-08-01 09:32:49 +00:00
}
timer.Stop();
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
timer.Reset();
2007-08-01 09:32:49 +00:00
Trace.Write("Timing OpenTK.OpenGL extension functions: ");
timer.Start();
for (int i = 0; ++i < num_calls; )
{
GL.ARB.ActiveTexture(GL.Enums.ARB_multitexture.TEXTURE0_ARB);
}
timer.Stop();
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
timer.Reset();
#endregion
2007-08-01 09:32:49 +00:00
#region DllImports
2007-08-01 09:32:49 +00:00
Trace.Write("Timing direct DllImport: ");
timer.Start();
for (int i = 0; ++i < num_calls; )
2007-08-01 09:32:49 +00:00
{
glVertex2f(0.0f, 0.0f);
}
timer.Stop();
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
timer.Reset();
Trace.Write("Timing direct DllImport (array): ");
timer.Start();
for (int i = 0; ++i < num_calls; )
{
glVertex2fv(v);
2007-08-01 09:32:49 +00:00
}
timer.Stop();
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
timer.Reset();
2007-08-01 09:32:49 +00:00
Trace.Write("Timing direct DllImport (void*): ");
timer.Start();
for (int i = 0; ++i < num_calls; )
{
glCallLists(v.Length, GL.Enums.ListNameType.FLOAT, v);
}
timer.Stop();
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
timer.Reset();
2007-08-01 09:32:49 +00:00
#endregion
2007-08-01 09:32:49 +00:00
}
}
public void InlineFunction()
{
++dummy_variable;
}
public virtual void VirtualFunction()
{
++dummy_variable;
}
[DllImport("opengl32.dll", EntryPoint = "glVertex2f"), SuppressUnmanagedCodeSecurity]
extern static void glVertex2f(float a, float b);
[DllImport("opengl32.dll", EntryPoint = "glVertex2fv"), SuppressUnmanagedCodeSecurity]
extern static void glVertex2fv(float[] v);
[DllImport("opengl32.dll", EntryPoint = "glCallLists"), SuppressUnmanagedCodeSecurity]
extern static void glCallLists(int count, GL.Enums.ListNameType type, object lists);
2007-08-01 09:32:49 +00:00
}
}