Skip to content

Instantly share code, notes, and snippets.

@daramkun
Created November 6, 2020 14:17
Show Gist options
  • Save daramkun/4a3f5d340c4782b117010c8f74f42f80 to your computer and use it in GitHub Desktop.
Save daramkun/4a3f5d340c4782b117010c8f74f42f80 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace FilePerformanceTest
{
class Program
{
private static readonly int FILES_COUNT = 10000;
private static readonly string Dir = "TestData";
static void MakeManyFiles()
{
var random = new Random();
if (!Directory.Exists(Dir))
Directory.CreateDirectory(Dir);
for (int i = 0; i < FILES_COUNT; ++i)
{
string filename = $"./{Dir}/{i:0000}.bytes";
if (File.Exists(filename))
continue;
StringBuilder builder = new StringBuilder();
var randomExiter = random.Next(0, 512);
while (builder.Length < 128 || random.Next(0, 512) != randomExiter)
builder.Append((char) ('A' + random.Next(24)));
var contents = builder.ToString();
File.WriteAllText(filename, contents);
}
}
static void MakeOneFileFromManyFiles()
{
if (File.Exists("Test.bytes"))
return;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < FILES_COUNT; ++i)
{
string filename = $"./{Dir}/{i:0000}.bytes";
string contents = File.ReadAllText(filename);
builder.AppendLine(filename);
builder.AppendLine(contents);
}
File.WriteAllText("Test.bytes", builder.ToString());
}
static void ParseFilesWithFirstLoads()
{
var files = new Dictionary<string, string>();
for (int i = 0; i < FILES_COUNT; ++i)
{
string filename = $"./{Dir}/{i:0000}.bytes";
string contents = File.ReadAllText(filename);
files.Add(filename, contents);
}
for (int i = 0; i < FILES_COUNT; ++i)
{
string filename = $"./{Dir}/{i:0000}.bytes";
foreach (var ch in files[filename])
;
}
}
static void ParseFilesPerLoop()
{
for (int i = 0; i < FILES_COUNT; ++i)
{
string filename = $"./{Dir}/{i:0000}.bytes";
string contents = File.ReadAllText (filename);
foreach (var ch in contents)
;
}
}
static void ParseFilesFromOneFile()
{
var files = new Dictionary<string, string> ();
var lines = File.ReadAllLines("Test.bytes");
for (int i = 0; i < lines.Length; i += 2)
files.Add(lines[i], lines[i + 1]);
for (int i = 0; i < FILES_COUNT; ++i)
{
string filename = $"./{Dir}/{i:0000}.bytes";
foreach (var ch in files [filename])
;
}
}
static void Main (string [] args)
{
Console.WriteLine("[Files Initialize...]");
MakeManyFiles();
MakeOneFileFromManyFiles();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Reset();
stopwatch.Start();
ParseFilesWithFirstLoads ();
stopwatch.Stop();
Console.WriteLine("Parse files with first loads: {0}", stopwatch.Elapsed);
stopwatch.Reset ();
stopwatch.Start ();
ParseFilesPerLoop ();
stopwatch.Stop ();
Console.WriteLine ("Parse files per loop: {0}", stopwatch.Elapsed);
stopwatch.Reset ();
stopwatch.Start ();
ParseFilesFromOneFile();
stopwatch.Stop ();
Console.WriteLine ("Parse files from one file: {0}", stopwatch.Elapsed);
}
}
}
@daramkun
Copy link
Author

daramkun commented Nov 6, 2020

image

image

image

Drive: WD BLUE SN550 1TB

image

Drive: SAMSUNG 860 EVO 1TB

image

Drive: SEAGATE FIRECUDA 2TB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment