(Gast)
n/a Beiträge
|
Re: Format-Funktion zu langsam
26. Jan 2005, 14:43
Ich habe es eben mal aus Jux (und aufgrund der Mittagspause ) in .Ne probiert.
Fazit: Es ist langsamer, aber es ist auch alles andere als hübsch oder optimiert.
Ich komme auf create array: 0.3967 ms
fill 8,000,000 random values: 427.5188 ms
conversion: 8054.1218 ms
write to disk: 13924.7499 ms
overall time: 22457.428 ms
Press any key to continue
Code:
using System;
using System.Collections;
using System.IO;
using RobertG.Invoked.ProcessInfo;
namespace DelphiPRAXIS.Jelly.Save3dArray
{
internal class Program
{
/// <summary>
/// Entry point
/// </summary>
/// <param name="args">Argumente (Nummer eins ist das zu schreibende Dateichen)</param>
[STAThread]
static void Main(string[] args)
{
Program program = new Program();
program.Run(args[0]);
}
PerformanceCounter counter;
double[,,] values;
public Program()
{
counter = new PerformanceCounter();
}
void Run(string fileName)
{
PerformanceCounter overallCounter = new PerformanceCounter();
overallCounter.Start();
values = CreateArray();
int valueCount = (int)values.LongLength;
FillRandomNumbers(valueCount);
ArrayList lines = new ArrayList(valueCount);
ConvertNumbers(lines);
SaveToFile(fileName, lines);
overallCounter.Stop();
Console.WriteLine("overall time: {0:0.####} ms",
overallCounter.Duration);
}
double[,,] CreateArray()
{
counter.Start();
double[,,] values = Array.CreateInstance(typeof (double),
new int[3] {200, 200, 200}) as double[,,];
counter.Stop();
Console.WriteLine("create array: {0:0.####} ms",
counter.Duration);
return values;
}
void FillRandomNumbers( int valueCount)
{
counter.Start();
Random random = new Random(valueCount);
for (int i = 0; i < values.GetLength(0); i++)
{
for (int j = 0; j < values.GetLength(1); j++)
{
for (int k = 0; k < values.GetLength(2); k++)
{
values[i, j, k] = random.NextDouble();
}
}
}
counter.Stop();
Console.WriteLine("fill {0:0,0} random values: {1:0.####} ms",
values.LongLength,
counter.Duration);
}
void ConvertNumbers(ArrayList lines)
{
counter.Start();
foreach (double value in values)
{
lines.Add(value.ToString("0.####"));
}
counter.Stop();
Console.WriteLine("conversion: {0:0.####} ms",
counter.Duration);
}
void SaveToFile(string fileName, IList lines)
{
FileStream stream = null;
counter.Start();
if (File.Exists(fileName))
{
stream = new FileStream(fileName,
FileMode.Truncate,
FileAccess.Write);
} else
{
stream = new FileStream(fileName,
FileMode.CreateNew,
FileAccess.Write);
}
BinaryWriter writer = new BinaryWriter(stream);
foreach (string line in lines)
{
writer.Write(line);
}
stream.Close();
counter.Stop();
Console.WriteLine("write to disk: {0:0.####} ms",
counter.Duration);
}
}
}
|
|
Zitat
|