BASIC 中的循环展开
我有一个嵌入式处理器,正在运行 BASIC (Parallax BASIC Stamp) 的精简版。在一个循环中,我通过 SPI 总线写入 1024 个值。
在编译语言中,通过展开循环(将更多语句放入循环中,降低开销与语句比率)可以获得更高的速度。但是,我不确定 BASIC,因为它是一种解释性语言,每个语句在执行之前都会被解释。
分析很困难,因为我必须找到可用的引脚,向其写入脉冲,然后用示波器进行测量。
从理论角度来看,BASIC 中的循环展开是否提供任何速度优势?
I have an embedded processor that is running a trimmed down version of BASIC (Parallax BASIC Stamp). In a loop, I am writing 1024 values via the SPI bus.
In compiled languages, more speed can be gained by unrolling the loop (putting more statements into the loop, reducing the overhead to statement ratio). However, I am not sure about BASIC since it is an interpretive language and each statement is interpreted before it is executed.
Profiling is difficult since I have to find an available pin, write a pulse to it, then measure with an o'scope.
From a theory point of view, does loop unrolling in BASIC provide any speed benefits?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理论上,循环展开可以减少循环内递增和比较所花费的时间。通过减少循环开销时间,可以提高性能。
解释型程序所获得的时间可能不如编译型程序那么重要。解释器获取指令、解释(构建代码)和执行语句的代码需要一定的时间开销。为了使循环展开时间节省显着,节省的时间必须大于此开销。
与微处理器不同,解释器可能没有针对执行速度进行优化。现代处理器具有高速缓存、分支预测和前瞻技术。有些甚至可以在执行其他指令时将新指令提取到缓存中。循环展开通过减少跳转次数并使执行更加可预测来利用这些功能。对于编译语言来说,这可以显着节省成本(对于大型迭代)。这种性能时间节省可能不适用于大多数解释器,因为他们可能不使用这些功能。
绩效改进的最佳确定是通过测量。就我而言,必须有足够的用户投诉才能证明执行测量的进度安排是合理的。
In theory, loop unrolling reduces the amount of time spent on incrementing and comparison within the loop. By virtue of reducing the loop overhead time, there is a performance gain.
The amount of time gained may not be as significant on an interpreted program as a compiled program. There is an overhead of time required for the interpreter to fetch an instruction, interpret (build code) and execute the code for a statement. In order for the loop unrolling time savings to be significant, the time savings must be larger than this overhead.
Unlike microprocessors, interpreters may not be optimized for execution speed. Modern processors have high speed caches, branch prediction and look-ahead techniques. Some can even fetch new instructions into the cache as others are executed. Loop unrolling takes advantage of these features by reducing the number of jumps and making the execution more predictable. For compiled languages, this adds a significant savings (for large iterations). This performance time savings may not be applicable to most interpreters since they may not employ these features.
The best determination of performance improvement is through measurement. In my case, there has to be enough user complaint to justify the schedule hit for performing the measurement.