gpt4 book ai didi

delphi - 为什么在Delphi中写入速度很慢?

转载 作者:行者123 更新时间:2023-12-03 19:10:58 24 4
gpt4 key购买 nike

我正在为我的A2计算项目设计一个应用程序,以模拟各种过滤器的响应。我遇到的一个问题是导出数据选项确实很慢。

通常,当生成要显示在显示器上的数据时,大约为40000-80000点/秒。当将其记录到文件中时,它下降到大约五分之一。

最初,我认为我的问题是因为我在每个数据点上都调用了writeln。因此,我编写了这样的代码,以便将数据排队到一个字符串中,并在一个大操作中每写入1000点。它使它稍快一些,但仍比内置窗体慢4-5倍。

为什么会这样呢?

这是导出代码:

   for xx := 0 to npoints do
begin
freq := minfreq + ((xx / npoints) * maxfreq);
ampl := GetAmplPoint(freq);
phase := GetPhasePoint(freq);
tempstr := tempstr + FormatFloat('#.#####', freq) + ',';
tempstr := tempstr + FormatFloat('#.#####', ampl) + ',';
tempstr := tempstr + FormatFloat('#.#####', phase) + sLineBreak;
// Queue up to 1000 points, then write the data in one lump:
// most of the time is spent in writeln waiting for IO which
// slows down export.
if xx mod 1000 = 0 then
begin
write(fileptr, tempstr);
tempstr := '';
ProgressBar.Position := 4 + Trunc((xx / npoints) * 96);
end;
end;

最佳答案

磁盘I / O是当今最慢的瓶颈之一,尤其是如果您使用慢速磁盘(即许多笔记本电脑上有4200/5400 rpm磁盘)。

使用缓冲I / O可以获得更好的性能(旧的Pascal I / O功能是很久以前设计的,可能使用较小的缓冲区,最好使用Delphi中当今可用的一种缓冲流)或异步I / O(通过缓冲区写入操作系统,调用将立即返回,稍后操作系统将告诉您何时写入数据。

关于delphi - 为什么在Delphi中写入速度很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8489503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com