Again how big are the files your loading in ?
If your reading from disk its physical hardware speed which is going to be slow. 2,000 rpm’s might sound good but then think about the ram’s Mega or Giga hertz speeds. 2k vs millions.
Pointers wont work until the io is completed.
Anyways …
blockbuffer copy and readallbytes, 300mb in a half to a quarter second.
Im guessing the buffer block copy is doing some magic tricks under the hood as well. I should point out file.readallbytes allocates and reads a huge block of memory straight in so big files can be a risk.
Edit: cleaned up the test.
// console test
using System;
using System.IO;
using System.IO.Compression;
using System.IO.MemoryMappedFiles;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestPerformanceTest01
{
class Program
{
public static Stopwatch stopWatch;
static int len = 150000000;
static void Main()
{
stopWatch = new Stopwatch();
Console.WriteLine("file : " + (len / 1000000) + " million short's" + " " + (len / 1000000 * 2) + " mb's");
Console.WriteLine(" ");
//W();
R0();
R1();
R2();
R3();
Console.WriteLine("done ");
Console.ReadKey();
}
static void W()
{
stopWatch.Start();
Random rnd = new Random();
short[] a = new short[len];
for (int i = 0; i < len; i++)
{
short s = (short)(rnd.Next(0, 1232));
a[i] = s;
}
using (BinaryWriter b = new BinaryWriter(File.Open("mytestfile.bin", FileMode.Create)))
{
foreach (short s in a)
{
b.Write(s);
}
}
stopWatch.Stop();
PrintOut("Write");
}
static void R3()
{
stopWatch.Start();
using (FileStream fs = new FileStream("mytestfile.bin", FileMode.Open))
{
using (BinaryReader b = new BinaryReader(fs))
{
int pos = 0;
int length = (int)b.BaseStream.Length;
while (pos < length)
{
short s = b.ReadInt16();
pos += 2;
}
}
}
stopWatch.Stop();
PrintOut("filestream and binary reader");
}
static void R2()
{
stopWatch.Start();
using (BinaryReader b = new BinaryReader(File.Open("mytestfile.bin", FileMode.Open)))
{
int pos = 0;
int length = (int)b.BaseStream.Length;
while (pos < length)
{
short s = b.ReadInt16();
pos +=2;
}
}
stopWatch.Stop();
PrintOut("file.open and binary reader");
}
static void R1()
{
stopWatch.Start();
byte[] b = File.ReadAllBytes("mytestfile.bin");
int len = b.Length / 2;
short[] s = new short[len];
Buffer.BlockCopy(b, 0, s, 0, len);
stopWatch.Stop();
PrintOut("File readallbytes and buffer.blockcopy");
}
private static void R0()
{
stopWatch.Start();
byte[] b = File.ReadAllBytes("mytestfile.bin");
int len = b.Length / 2;
short[] s = new short[len];
for (int i = 0; i < len; i += 2)
s[i] = (short)(b[i] + b[(i + 1)] << 8);
//s[i] = (short)((b[i]) + (b[(i + 1)] << 8));
stopWatch.Stop();
PrintOut("File readallbytes and bit shift");
}
public static void PrintOut(string msg)
{
TimeSpan ts = stopWatch.Elapsed;
Console.WriteLine(msg);
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
Console.WriteLine("");
stopWatch.Reset();
}
}
}
output…
A small file.
file : 10 million short’s 20 mb’s
Write
RunTime 00:00:00.73
File readallbytes and bit shift
RunTime 00:00:00.04
File readallbytes and buffer.blockcopy
RunTime 00:00:00.02
file.open and binary reader
RunTime 00:00:00.36
filestream and binary reader
RunTime 00:00:00.37
done
small movie sized file
file : 150 million short’s 300 mb’s
File readallbytes and bit shift
RunTime 00:00:00.67
File readallbytes and buffer.blockcopy
RunTime 00:00:00.26
file.open and binary reader
RunTime 00:00:04.75
filestream and binary reader
RunTime 00:00:05.01
done
a gigabyte file
file : 500 million short’s 1000 mb’s
Write
RunTime 00:00:39.02
File readallbytes and bit shift
RunTime 00:00:02.71
File readallbytes and buffer.blockcopy
RunTime 00:00:01.41
file.open and binary reader
RunTime 00:00:17.08
filestream and binary reader
RunTime 00:00:15.08
done