Exemple #1
0
 private static void MemoryMappedFile()
 {
     //IF RUNNING IN MONO REMEMBER THAT YOU HAVE TO EXPORT LD_LIBRARY_PATH TO POINT TO MONO LIB FOLDER FOR POSIXHELPER.SO
     Console.WriteLine("Memory Mapped File.");
     var filename = @"fofadasfddsho";
     var size = 6000 * MEGABYTE;
     using(var file = File.Open(filename, FileMode.OpenOrCreate)) {
         file.SetLength(size + 1);
     }
     using (
         var memmap = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(filename, FileMode.OpenOrCreate, "MyMap23", size,
                                                      MemoryMappedFileAccess.ReadWrite))
     {
         using (var view = memmap.CreateViewAccessor(0, size, MemoryMappedFileAccess.ReadWrite))
         {
             var watch = new Stopwatch();
             watch.Start();
             var filter = new BloomFilter(view.SafeMemoryMappedViewHandle.DangerousGetHandle(), size/4,
                                          new IHasher[] {new Murmur3AUnsafe(), new XXHashUnsafe()});
             for (int i = 0; i < 400000; i++)
             {
                 var bytes = Guid.NewGuid().ToByteArray();
                 filter.Add(bytes);
                 if (!filter.Contains(bytes))
                 {
                     throw new Exception("broken");
                 }
                 if (i%100000 == 0)
                 {
                     Console.Write(".");
                 }
             }
             Console.WriteLine();
             Console.WriteLine(watch.Elapsed);
         }
     }
     File.Delete(filename);
 }
Exemple #2
0
        private static unsafe void UnmanagedMemory()
        {
            Console.WriteLine("Unmanaged Memory");
            const int size = (int) (500*MEGABYTE);

            var watch = new Stopwatch();
            watch.Start();
            var ptr = Marshal.AllocHGlobal(size);
            var filter = new BloomFilter(ptr, size/4,
                                         new IHasher[] {new Murmur3AUnsafe(), new XXHashUnsafe()});
            for (int i = 0; i < 1000000; i++)
            {
                var bytes = Guid.NewGuid().ToByteArray();
                filter.Add(bytes);
                if (!filter.Contains(bytes))
                {
                    throw new Exception("broken");
                }
                if (i%100000 == 0)
                {
                    Console.Write(".");
                }
            }
            Console.WriteLine();
            Console.WriteLine(watch.Elapsed);
            Marshal.FreeHGlobal(ptr);
        }