2007-08-10 11:27:13 +02:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
2007-08-01 11:32:49 +02:00
|
|
|
|
using System.Text;
|
2007-10-17 13:32:36 +02:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Diagnostics;
|
2007-08-01 11:32:49 +02:00
|
|
|
|
using System.Windows.Forms;
|
2008-01-31 15:23:20 +01:00
|
|
|
|
using System.Security;
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2008-02-02 01:58:26 +01:00
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
|
using OpenTK.Graphics.OpenGL.Enums;
|
2008-01-31 15:23:20 +01:00
|
|
|
|
using OpenTK.Graphics;
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
|
|
|
|
namespace Examples.Tests
|
|
|
|
|
{
|
2007-10-17 13:32:36 +02:00
|
|
|
|
public class S01_Call_Performance : IExample
|
2007-08-01 11:32:49 +02:00
|
|
|
|
{
|
2007-10-17 13:32:36 +02:00
|
|
|
|
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 11:32:49 +02:00
|
|
|
|
{
|
2008-03-24 14:26:25 +01:00
|
|
|
|
using (OpenTK.GLControl control = new OpenTK.GLControl(GraphicsMode.Default))
|
2007-10-17 13:32:36 +02:00
|
|
|
|
{
|
|
|
|
|
Trace.WriteLine(String.Format("Number of calls: {0}", num_calls));
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
Stopwatch timer = new Stopwatch();
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
#region Managed functions
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2007-10-17 13:32:36 +02: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 11:32:49 +02:00
|
|
|
|
|
2007-10-17 13:32:36 +02: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 11:32:49 +02:00
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
Trace.Write("Timing virtual .Net functions: ");
|
|
|
|
|
timer.Start();
|
|
|
|
|
for (int i = 0; ++i < num_calls; )
|
2007-08-01 11:32:49 +02:00
|
|
|
|
{
|
2007-10-17 13:32:36 +02:00
|
|
|
|
VirtualFunction();
|
2007-08-01 11:32:49 +02:00
|
|
|
|
}
|
2007-10-17 13:32:36 +02:00
|
|
|
|
timer.Stop();
|
|
|
|
|
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
|
|
|
|
|
timer.Reset();
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
#endregion
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2008-02-02 01:58:26 +01:00
|
|
|
|
#region OpenTK.Graphics.OpenGL
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2008-02-02 01:58:26 +01:00
|
|
|
|
Trace.Write("Timing OpenTK.Graphics.OpenGL core functions: ");
|
2007-10-17 13:32:36 +02:00
|
|
|
|
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 11:32:49 +02:00
|
|
|
|
|
2008-02-02 01:58:26 +01:00
|
|
|
|
Trace.Write("Timing OpenTK.Graphics.OpenGL core functions (array): ");
|
2007-10-17 13:32:36 +02:00
|
|
|
|
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 11:32:49 +02:00
|
|
|
|
|
2008-02-02 01:58:26 +01:00
|
|
|
|
Trace.Write("Timing OpenTK.Graphics.OpenGL core functions (void*): ");
|
2007-10-17 13:32:36 +02:00
|
|
|
|
timer.Start();
|
|
|
|
|
for (int i = 0; ++i < num_calls; )
|
2007-11-04 16:32:24 +01:00
|
|
|
|
GL.CallLists(v.Length, ListNameType.Float, v);
|
2007-10-17 13:32:36 +02:00
|
|
|
|
timer.Stop();
|
|
|
|
|
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
|
|
|
|
|
timer.Reset();
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2008-02-02 01:58:26 +01:00
|
|
|
|
Trace.Write("Timing OpenTK.Graphics.OpenGL extension functions: ");
|
2007-10-17 13:32:36 +02:00
|
|
|
|
timer.Start();
|
|
|
|
|
for (int i = 0; ++i < num_calls; )
|
2008-01-04 21:55:33 +01:00
|
|
|
|
GL.ActiveTexture(TextureUnit.Texture0);
|
2007-11-04 16:32:24 +01:00
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
timer.Stop();
|
|
|
|
|
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
|
|
|
|
|
timer.Reset();
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
#region DllImports
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
Trace.Write("Timing direct DllImport: ");
|
|
|
|
|
timer.Start();
|
|
|
|
|
for (int i = 0; ++i < num_calls; )
|
2007-08-01 11:32:49 +02:00
|
|
|
|
{
|
2007-10-17 13:32:36 +02: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 11:32:49 +02:00
|
|
|
|
}
|
2007-10-17 13:32:36 +02:00
|
|
|
|
timer.Stop();
|
|
|
|
|
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
|
|
|
|
|
timer.Reset();
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2007-10-20 12:29:39 +02:00
|
|
|
|
GL.GenLists(1);
|
2007-10-17 13:32:36 +02:00
|
|
|
|
Trace.Write("Timing direct DllImport (void*): ");
|
|
|
|
|
timer.Start();
|
|
|
|
|
for (int i = 0; ++i < num_calls; )
|
|
|
|
|
{
|
2007-11-04 16:32:24 +01:00
|
|
|
|
glCallLists(v.Length, ListNameType.Float, v);
|
2007-10-17 13:32:36 +02:00
|
|
|
|
}
|
|
|
|
|
timer.Stop();
|
|
|
|
|
Trace.WriteLine(String.Format("{0} ns", timer.Elapsed.TotalMilliseconds * (1000000.0 / (double)num_calls)));
|
|
|
|
|
timer.Reset();
|
2007-08-01 11:32:49 +02:00
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
#endregion
|
2007-08-01 11:32:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-04 14:09:58 +02:00
|
|
|
|
|
2007-10-20 12:29:39 +02:00
|
|
|
|
public static readonly int order = 1;
|
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
public void InlineFunction()
|
|
|
|
|
{
|
|
|
|
|
++dummy_variable;
|
|
|
|
|
}
|
2007-08-04 14:09:58 +02:00
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
public virtual void VirtualFunction()
|
2007-08-04 14:09:58 +02:00
|
|
|
|
{
|
2007-10-17 13:32:36 +02:00
|
|
|
|
++dummy_variable;
|
2007-08-04 14:09:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-10-17 13:32:36 +02:00
|
|
|
|
[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]
|
2007-11-04 16:32:24 +01:00
|
|
|
|
extern static void glCallLists(int count, ListNameType type, object lists);
|
2007-10-17 13:32:36 +02:00
|
|
|
|
|
2007-08-01 11:32:49 +02:00
|
|
|
|
}
|
2007-08-10 11:27:13 +02:00
|
|
|
|
}
|