Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Created July 28, 2017 12:21
Show Gist options
  • Save EgorBo/71c3683b586103517f9b5280593293e5 to your computer and use it in GitHub Desktop.
Save EgorBo/71c3683b586103517f9b5280593293e5 to your computer and use it in GitHub Desktop.
test.cs
[DllImport("foo", CallingConvention = CallingConvention.Cdecl)]
static extern int GetHashNative(string str);
static int GetHashManaged(string str)
{
int hash = 0;
if (string.IsNullOrEmpty(str))
return hash;
for (int i = 0; i < str.Length; i++)
hash = char.ToLower(str[i]) + (hash << 6) + (hash << 16) - hash;
return hash;
}
static void Main()
{
string words = "qwertyuiopasdfghjklzxcvbnm_123";
string w = "";
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < 1000; i++)
{
w += words[i % words.Length];
var code = GetHashManaged(w);
//var code = GetHashNative(w);
}
sw.Stop();
System.Console.WriteLine("elapsed: " + sw.ElapsedMilliseconds);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment