程序员应该知道的延迟数

数量级(时间)

纳秒(ns):\(10^{-9}\)

  • 1 纳秒 -- 1GHz 的 CPU 的时钟周期,该周期的无线电波波长 0.3 米。
  • 3.3 纳秒 -- 光传播一米所使用的时间。

微秒(µs):\(10^{-6}\)

1 微秒(microsecond)即 1000 纳秒。

  • 1 微秒 -- 商业高速频闪观测器的闪动时间。
  • 50 微秒 -- 人能够听到的最高频率的声音的周期(20KHz)。
  • 125 微秒 -- 电话声音的采样间隔。(8KHz)

毫秒(ms):\(10^{-3}\)

1 毫秒 (millisecond) 即 1000 微秒。

  • 1 毫秒 -- 大部分电脑计算时间的基本单位。许多程式计算延迟的基本单位。
  • 3 毫秒 -- 苍蝇 搧一次翅膀的时间。
  • 5 毫秒 -- 蜜蜂 搧一次翅膀的时间。

秒(s)

国际单位制中时间的基本单位,本页面的单位基准。1 秒即 1000 毫秒。

一般很少使用比秒更大的字头单位(如千秒等),而用小时、日、年 等。

  • 60 秒 -- 1 分钟的长度。

计算机执行一条指令需要多久

计算机执行一条指令需要多久,到内存中取一个字需要多久(缓存是否击中), 到磁盘读取连续的字需要多久,而磁盘的定位又需要多久。

Approximate timing for various operations on a typical PC:

operation time
execute typical instruction 1/1,000,000,000 sec = 1 nanosec
fetch from L1 cache memory 1 nanosec
branch misprediction 5 nanosec
fetch from L2 cache memory 7 nanosec
Mutex lock/unlock 25 nanosec
fetch from main memory 100 nanosec
send 2K bytes over 1Gbps network 20,000 nanosec
read 1MB sequentially from memory 250,000 nanosec
fetch from new disk location (seek) 8,000,000 nanosec
read 1MB sequentially from disk 20,000,000 nanosec
send packet US to Europe and back 150 milliseconds = 150,000,000 nanosec

在典型PC上各种操作的近似时间:

操作 时间
执行典型指令 1/1,000,000,000 sec = 1 nanosec
从一级缓存中读取数据 1 nanosec
分支预测错误 5 nanosec
从二级缓存中读取数据 7 nanosec
互斥锁定/解锁 25 nanosec
从主存储器中读取数据 100 nanosec
通过 1Gbps 网络发送 2K 字节 20,000 nanosec
从内存顺序读取 1MB 数据 250,000 nanosec
从新磁盘位置读取数据(定位) 8,000,000 nanosec
从磁盘顺序读取 1MB 数据 20,000,000 nanosec
在美国向欧洲发送包并返回 150 milliseconds = 150,000,000 nanosec

1985~2015 年

1985~2015 年随着硬件设备的进展,访问延时的变化

The gap between disk, DRAM, and CPU speeds
Figure 1: The gap between disk, DRAM, and CPU speeds

2020 年

Latency Numbers Every Programmer Should Know
Figure 2: Latency Numbers Every Programmer Should Know

参考

徐宥 十年学会程序设计

Peter Norvig Teach Yourself Programming in Ten Years

Computer Systems: A Programmer's Perspective, 3/E (CS:APP3e) Figure 6.16: The gap between disk, DRAM, and CPU speeds.

Colin Scott Latency Numbers Every Programmer Should Know

posted @ 2020-04-18 02:35  clipboard  阅读(1566)  评论(0编辑  收藏  举报