整个过程有两件事负责——
- CPU
- IO 设备
最初,当您将行从一个文件复制到另一个文件时,该过程中不涉及任何 IO 设备(因为您没有打印任何行)。因此,CPU 没有等待时间。因此,该过程在更短的时间内完成。
当你引入 println 语句时,对于 CPU 从第一个文件中读取的每一行,它必须将该行发送到 IO 设备进行打印。将线路发送到 IO 设备后,CPU 必须等待它完成工作。一旦 IO 设备完成打印,CPU 将继续该过程并将该行粘贴到第二个文件中。这个过程将一直持续到第一个文件中的所有行都复制到第二个文件中为止。
因此,对于文件中的每一行,CPU 必须等待一定的时间。这是处理时间增加的第一个原因。其次,IO 打印会创建文件的另一个副本。所以,现在您要创建 2 个副本而不是 1 个。
46,000,000 行?
Let's take the time taken by each operation in the process is 1 microsecond.Initially -
Time took by CPU to copy a line = 1 microsecond
Time took by CPU to paste a line = 1 microsecond
Total time for copying 46,000,000 lines = 46,000,000 microsecond = 46 sec
Total time for pasting 46,000,000 lines = 46,000,000 microsecond = 46 sec
Total time of the entire process = 92 sec.
Println 介绍
Time took by CPU to copy a line = 1 microsecond
Time took by IO to paste a line = 1 microsecond
Time took by CPU to paste a line = 1 microsecond
Waiting time of CPU for every line = 1 microsecond
Total time taken by CPU for copying 46,000,000 lines = 46,000,000 microsecond = 46 sec
Total time taken by IO for copying 46,000,000 lines = 46,000,000 microsecond = 46 sec
Waiting time of CPU = 46,000,000 microsecond = 46 sec
Total time by CPU for pasting 46,000,000 lines = 46,000,000 microsecond = 46 sec
Total time of the entire process = 184 sec.
希望这能回答您的问题!