Skip to content

Instantly share code, notes, and snippets.

@corruptmem
Created August 29, 2010 00:01
Show Gist options
  • Save corruptmem/555736 to your computer and use it in GitHub Desktop.
Save corruptmem/555736 to your computer and use it in GitHub Desktop.
using System;
using System.CodeDom.Compiler;
using System.IO;
namespace OptimisedAdd
{
public static class Math
{
// Optimised version of the + operator.
public static void Add(int left, int right, out int result)
{
string doc = "using System.IO; public class Add { public void Main() { File.WriteAllText(@\"C:\\\\result.txt\",(" + left + "+" + right + ").ToString()); }} ";
CompilerParameters cparams = new CompilerParameters();
CompilerResults cr = CodeDomProvider.CreateProvider("c#").CompileAssemblyFromSource(cparams, doc);
object instance = cr.CompiledAssembly.CreateInstance("Add");
instance.GetType().GetMethod("Main").Invoke(instance, null);
string returnedresult = File.ReadAllText(@"C:\result.txt");
result = int.Parse(returnedresult);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment