【计算机系统结构】Instruction path length 指令路径长度

Instruction path length
在计算机性能中,Instruction path length(指令路径长度)是指在计算机程序中需要执行的一部分机器代码指令数量。对于整个程序而言,总路径长度可以被认为是衡量在特定计算机硬件上的算法性能。路径长度在一个简单的条件指令通常被认为是等于2,一个指令用于执行比较,如果特殊的条件满足另一个用于执行分支。每一个指令执行时间的长度通常不被认为是决定路径长度。因此路径长度仅仅是相关性能的一个指示,并不是所有场景的解决方案。

在执行基准测试程序时,绝大多数指令路径长度通常在程序的内部循环内。

在介绍cache 之前,路径长度认为是运行时间的近似。但在带有cache的现代处理器,路径长度认为是非常差的近似,因为当数据不在cache内,加载指令会花费几百个周期。当数据在cache 内时,顺序迁移会更快(甚至在循环中的另一轮相同的指令)。

汇编程序。

因为典型的汇编指令与机器指令是一对一的关系,指令路径长度频繁被作为执行代码中一段特定的部分或一个函数的汇编指令的数量。执行一个1000个元素的无序列表查表操作需要约2000个机器指令(平均,假设输入服从均匀分布)。而用二分查找在有序列表执行相同的查表操作只需要40个机器指令,这是非常可观的性能提升。以指令路径长度表示,这种度量在这个例子中减少50倍。这就是实际指令用时比起一个更短的指令路径长度算法而言只是第二考虑的因素的原因。

汇编语言程序的指令路径长度通常与该程序的源代码行数大不相同,因为指令路径长度仅包括用于给定输入的已执行控制流中的代码,而不包括不相关的代码。对于特定的输入,或无法到达的代码。

高级语言程序。
因为有一种说法:用高级语言写程序会导致更多倍的机器指令。没有一个指令集模拟器的情况下确定指令路径长度并不总是可行的。指令集模拟器可计算执行指令数量。如果高级语言支持并可产生一个汇编列表,那么通过检查这个列表来估计指令路径长度是可能的。

确定指令路径长度的因素。
1 内联代码 VS 包含相同状态的函数/方法的返回与调用。
2 无序列表中项的顺序。最经常出现的项应放在第一位以避免长搜索。
3 选择算法- 下标,二分,或线性搜索
4 重新计算 VS 保留较早计算(备忘)——可以减少多次复杂迭代
5 一次性将表格读入内存 VS 每一次额外的重新读入,避免通过I/O方法调用的高路径长度。

指令路径的用途
1 在任何编程语言中对程序选择一个近似算法来最小化总体路径长度
2 在任何编程语言中监控程序被优化的情况
3 在任何高级编程语言中确定特定的高级语言表达效率是如何
4 作为一个总体计算机性能的近似测量

Wiki 原文:

Instruction path length

This article does not cite any sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (November 2016) (Learn how and when to remove this template message)
In computer performance, the instruction path length is the number of machine code instructions required to execute a section of a computer program. The total path length for the entire program could be deemed a measure of the algorithm’s performance on a particular computer hardware. The path length of a simple conditional instruction would normally be considered as equal to 2,[citation needed] one instruction to perform the comparison and another to take a branch if the particular condition is satisfied. The length of time to execute each instruction is not normally considered in determining path length and so path length is merely an indication of relative performance rather than in any sense absolute.

When executing a benchmark program, most of the instruction path length is typically inside the program’s inner loop.

Before the introduction of caches, the path length was an approximation of running time, but in modern CPUs with caches, it can be a much worse approximation, with some load instructions taking hundreds of cycles when the data is not in cache, or orders of magnitude faster when in cache (even the same instruction in another round in a loop).

Assembly programs
Since there is, typically, a one-to-one relationship between assembly instructions and machine instructions, the instruction path length is frequently taken as the number of assembly instructions required to perform a function or particular section of code. Performing a simple table lookup on an unsorted list of 1,000 entries might require perhaps 2,000 machine instructions (on average, assuming uniform distribution of input values), while performing the same lookup on a sorted list using a binary search algorithm might require only about 40 machine instructions, a very considerable saving. Expressed in terms of instruction path length, this metric would be reduced in this instance by a massive factor of 50 – a reason why actual instruction timings might be a secondary consideration compared to a good choice of algorithm requiring a shorter path length.

The instruction path length of an assembly language program is generally vastly different than the number of source lines of code for that program, because the instruction path length includes only code in the executed control flow for the given input and does not include code that is not relevant for the particular input, or unreachable code.

High-level language (HLL) programs
Since one statement written in a high-level language can produce multiple machine instructions of variable number, it is not always possible to determine instruction path length without, for example, an instruction set simulator – that can count the number of ‘executed’ instructions during simulation. If the high-level language supports and optionally produces an ‘assembly list’, it is sometimes possible to estimate the instruction path length by examining this list.

Factors determining instruction path length

  • in-line code versus the overheads of calling and returning from a
    function, procedure, or method containing the same statements
  • order of items in unsorted lookup list – most frequently occurring
    items should be placed first to avoid long searches
  • choice of algorithm – indexed, binary or linear (item-by-item) search
  • calculate afresh versus retain earlier calculated (memoization) – may
    reduce multiple complex iterations
  • read some tables into memory once versus external read afresh each
    time – avoiding high path length through multiple I/O function calls

Use of instruction path lengths
From the above, it can be realized that knowledge of instruction path lengths can be used:

  • to choose an appropriate algorithm to minimize overall path lengths
    for programs in any language

  • to monitor how well a program has been optimized in any language

  • to determine how efficient particular HLL statements are for any HLL
    language

  • as an approximate measure of overall computer performance

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值