using System;
using System.Threading;
using System.IO;
using System.Diagnostics;
namespace TEST
{
internal class Program
{
private static void Main(string[] args)
{
var stopwatch = Stopwatch.StartNew();
int filee = 0;
for(int i = 1; i <= 1000; i++)
{
int f = filee;
f += i;
filee = f;
}
Console.WriteLine(stopwatch.Elapsed);
stopwatch.Restart();
File.WriteAllText("file.txt", "0");
for(int i = 1; i <= 1000; i++)
{
int f = int.Parse(File.ReadAllText("file.txt"));
f += i;
File.WriteAllText("file.txt", f.ToString());
}
Console.WriteLine(stopwatch.Elapsed);
}
}
}
출력:
00:00:00.0000098
00:00:04.1014096
런타임 중에 파일을 썼다 읽었다하는 것은 상당히 비효율적이다. File IO를 최대한 줄이자.
'C#' 카테고리의 다른 글
[C#] 서버 시간 가져오기 (0) | 2023.10.18 |
---|---|
lock (1) | 2023.07.02 |
class vs struct (0) | 2023.05.18 |
[C# Windows] System.IO.Path.GetTempPath 쓰지 마라. (0) | 2023.05.09 |
garbage collect의 함정 (0) | 2023.03.25 |
댓글