1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

大きなサイズのファイルを高速に作る方法

Posted at

はじめに

空のファイルを作る時、どのようなコマンドを使っているでしょうか?
私の場合は以下のように、ファイルサイズが0であればtouchコマンド、一定のサイズのファイルが必要な場合はddコマンドを使っていました。

ところがddコマンドとは別に、一定のサイズのファイルを作る方法としてfallocateというコマンドがあることを知ったので、両者のパフォーマンスを比較してみました。

[nkojima@akagi sample_files]$ touch sample.txt
[nkojima@akagi sample_files]$ dd if=/dev/zero of=./sample2.txt bs=1K count=1024
1024+0 レコード入力
1024+0 レコード出力
1048576 バイト (1.0 MB) コピーされました、 0.00580267 秒、 181 MB/秒
[nkojima@akagi sample_files]$ ls -lah
合計 1.0M
drwxrwxr-x  2 nkojima nkojima   43  6月 21 12:18 .
drwx------. 5 nkojima nkojima  175  6月 21 12:17 ..
-rw-rw-r--  1 nkojima nkojima    0  6月 21 12:17 sample.txt
-rw-rw-r--  1 nkojima nkojima 1.0M  6月 21 12:18 sample2.txt

テストに使用した環境

  • CPU
  • Kabylake: Pentium G4560
  • マザーボード:ASRock H110M-STX
  • メモリ:8GB(4GB×2枚)
  • ストレージ:256GB SSD(NVMe)
  • OS
  • CentOS 7.8(2003)

テスト方法

  • ddコマンドとfallocateコマンドを使って、様々なサイズのファイルを作成して、その処理速度を測定しました。
  • ファイルサイズ:100MB、500MB、1GB、2GB、4GB、8GB、16GB
  • ddコマンドのオプションのブロックサイズ(-bs)は1MBとしました。
  • 処理時間の測定には、以下のようにdateコマンドを使ったため、正確な処理時間でありません。
  • 処理を3回実行して、その平均値をテスト結果としました。
[root@akagi ~]# date; dd if=/dev/zero of=dd.txt bs=1M count=100; date;
2020年  6月 21日 日曜日 12:48:28 JST
100+0 レコード入力
100+0 レコード出力
104857600 バイト (105 MB) コピーされました、 0.0362178 秒、 2.9 GB/秒
2020年  6月 21日 日曜日 12:48:29 JST
[root@akagi ~]# date; fallocate -l 100m ./fallocate.txt; date;
2020年  6月 21日 日曜日 12:48:52 JST
2020年  6月 21日 日曜日 12:48:52 JST

テスト結果

  • 以下の表を見れば一目瞭然、圧倒的にfallocateコマンドの方が高速でした。
  • swap領域を作る時など、単一の大きなファイルを作る必要があるケースでは、特にfallocateコマンドが有用だと考えられます。
  • ddコマンドは高速なストレージ(NVMe-SSD)を使ってもそれなりに処理時間がかかるので、低速なストレージだとさらに大きな差がつくのではないかと思います。
ファイルサイズ ddコマンドの処理時間(秒) fallocateコマンドの処理時間(秒)
100MB <1 <1
500MB <1 <1
1GB 1 <1
2GB 2 <1
4GB 4 <1
8GB 7 <1
16GB 15 <1

関連記事

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?