考虑下面的代码。这段代码应该以固定的速率处理数据,在一秒钟的批处理中,它是整个系统的一部分,不会占用太多时间。
当运行超过 100 批 1 秒的数据时,程序需要 35 秒(或 35%),循环执行此函数。测试循环是专门用 Ada.RealTime 计时的。数据是预先生成的,因此大部分执行时间肯定在这个循环中。
如何改进代码以将处理时间降至最低?
代码将在 Intel Pentium-M 上运行,它是带有 SSE2 的 P3。
package FF is new Ada.Numerics.Generic_Elementary_Functions(Float);
N : constant Integer := 820;
type A is array(1 .. N) of Float;
type A3 is array(1 .. 3) of A;
procedure F(state : in out A3;
result : out A3;
l : in A;
r : in A) is
s : Float;
t : Float;
begin
for i in 1 .. N loop
t := l(i) + r(i);
t := t / 2.0;
state(1)(i) := t;
state(2)(i) := t * 0.25 + state(2)(i) * 0.75;
state(3)(i) := t * 1.0 /64.0 + state(2)(i) * 63.0 /64.0;
for r in 1 .. 3 loop
s := state(r)(i);
t := FF."**"(s, 6.0) + 14.0;
if t > MAX then
t := MAX;
elsif t < MIN then
t := MIN;
end if;
result(r)(i) := FF.Log(t, 2.0);
end loop;
end loop;
end;
用于测试的伪代码
create two arrays of 80 random A3 arrays, called ls and rs;
init the state and result A3 array
record the realtime time now, called last
for i in 1 .. 100 loop
for j in 1 .. 80 loop
F(state, result, ls(j), rs(j));
end loop;
end loop;
record the realtime time now, called curr
output the duration between curr and last