H
Henrik Schmid
Hi,
consider the attached code.
Serializing the multi-dimensional array takes about 36s
vs. 0.36s for the single-dimensional array.
Initializing the multi-dimensional array takes about 4s
vs. 0.3s for the single-dimensional array.
(I know initializing is not necessary in this simple example,
but in my application it was necessary to frequently
re-initialize an array)
Are there any workarounds other than using single-dimensional arrays,
store the array bounds in additional fields (in the actual code the
arrays are not always zero based) and do the index calculations in code?
TIA,
Henrik
byte[, ,] multiDimArray = new byte[1000, 1000, 64];
byte[] singleDimArray = new byte[64000000];
DateTime start = DateTime.Now;
using (Stream stream = File.Open(@"d:\test.tst", FileMode.OpenOrCreate))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, multiDimArray);
}
Console.WriteLine("Serialize multi dim " + (DateTime.Now -
start).TotalSeconds);
start = DateTime.Now;
using (Stream stream = File.Open(@"d:\test.tst", FileMode.OpenOrCreate))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, singleDimArray);
}
Console.WriteLine("Serialize single dim " + (DateTime.Now -
start).TotalSeconds);
start = DateTime.Now;
for (int i = multiDimArray.GetLowerBound(0); i <=
multiDimArray.GetUpperBound(0); ++i)
for (int j = multiDimArray.GetLowerBound(1); j <=
multiDimArray.GetUpperBound(1); ++j)
for (int k = multiDimArray.GetLowerBound(2); k <=
multiDimArray.GetUpperBound(2); ++k)
multiDimArray[i, j, k] = 0;
Console.WriteLine("Init multi dim " + (DateTime.Now - start).TotalSeconds);
start = DateTime.Now;
for (int i = 0; i < singleDimArray.Length; ++i)
singleDimArray = 0;
Console.WriteLine("Init single dim " + (DateTime.Now - start).TotalSeconds);
consider the attached code.
Serializing the multi-dimensional array takes about 36s
vs. 0.36s for the single-dimensional array.
Initializing the multi-dimensional array takes about 4s
vs. 0.3s for the single-dimensional array.
(I know initializing is not necessary in this simple example,
but in my application it was necessary to frequently
re-initialize an array)
Are there any workarounds other than using single-dimensional arrays,
store the array bounds in additional fields (in the actual code the
arrays are not always zero based) and do the index calculations in code?
TIA,
Henrik
byte[, ,] multiDimArray = new byte[1000, 1000, 64];
byte[] singleDimArray = new byte[64000000];
DateTime start = DateTime.Now;
using (Stream stream = File.Open(@"d:\test.tst", FileMode.OpenOrCreate))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, multiDimArray);
}
Console.WriteLine("Serialize multi dim " + (DateTime.Now -
start).TotalSeconds);
start = DateTime.Now;
using (Stream stream = File.Open(@"d:\test.tst", FileMode.OpenOrCreate))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, singleDimArray);
}
Console.WriteLine("Serialize single dim " + (DateTime.Now -
start).TotalSeconds);
start = DateTime.Now;
for (int i = multiDimArray.GetLowerBound(0); i <=
multiDimArray.GetUpperBound(0); ++i)
for (int j = multiDimArray.GetLowerBound(1); j <=
multiDimArray.GetUpperBound(1); ++j)
for (int k = multiDimArray.GetLowerBound(2); k <=
multiDimArray.GetUpperBound(2); ++k)
multiDimArray[i, j, k] = 0;
Console.WriteLine("Init multi dim " + (DateTime.Now - start).TotalSeconds);
start = DateTime.Now;
for (int i = 0; i < singleDimArray.Length; ++i)
singleDimArray = 0;
Console.WriteLine("Init single dim " + (DateTime.Now - start).TotalSeconds);