并行处理字符串Delphi的全部可用CPU使用率 [英] Parallel processing strings Delphi full available CPU usage

查看:164
本文介绍了并行处理字符串Delphi的全部可用CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是在单个Delphi应用程序中将浮点数转换为字符串时,充分利用可用内核。我认为这个问题适用于字符串的一般处理。但是在我的示例中,我专门使用了FloatToStr方法。

The goal is to achieve full usage of the available cores, in converting floats to strings in a single Delphi application. I think this problem applies to the general processing of string. Yet in my example I am specifically using the FloatToStr method.

我在做什么(我将其保持为非常简单,因此在实现方面几乎没有歧义):

What I am doing (I've kept this very simple so there is little ambiguity around the implementation):


  • 使用Delphi XE6

  • 创建从TThread继承的线程对象,然后启动它们。

  • 在线程执行过程中,它将通过FloatToStr方法将大量的
    双打转换为字符串。

  • 为简化起见,这些双精度变量是相同的常数,因此线程不需要
    共享或全局内存资源。

  • Using Delphi XE6
  • Create thread objects which inherit from TThread, and start them.
  • In the thread execute procedure it will convert a large amount of doubles into strings via the FloatToStr method.
  • To simplify, these doubles are just the same constant, so there is no shared or global memory resource required by the threads.

尽管使用了多个内核,但CPU使用率%始终会在单个内核数量上达到最大。我了解这是一个已确定的问题。因此,我有一些特定的问题。

Although multiple cores are used, the CPU usage % always will max out on the amount of a single core. I understand this is an established issue. So I have some specific questions.

通过简单的方法,多个应用程序实例可以完成相同的操作,从而更充分地利用可用的CPU。是否可以在同一可执行文件中有效地执行此操作?
即在OS级别上为线程分配不同的进程ID或OS识别的等效分区?还是在开箱即用的Delphi中根本不可能?

In a simple way the same operation could be done by multiple app instances, and thereby achieve more full usage of the available CPU. Is it possible to do this effectively within the same executable ? I.e. assign threads different process ids on the OS level or some equivalent division recognised by the OS ? Or is this simply not possible in out of the box Delphi ?

作用域内:
我知道有不同的内存管理器可用其他小组已尝试更改一些较低级别的asm锁定用法 http://synopse.info/forum/viewtopic.php ?id = 57
但是,我问这个问题的范围是不要把事情做得这么低。

On scope : I know there are different memory managers available & other groups have tried changing some of the lower level asm lock usage http://synopse.info/forum/viewtopic.php?id=57 But, I am asking this question in the scope of not doing things at such a low level.

谢谢

J。我的代码故意非常简单:

Hi J. My code is deliberately very simple :

TTaskThread = class(TThread)
public
  procedure Execute; override;
end;

procedure TTaskThread.Execute;
var
  i: integer;
begin
  Self.FreeOnTerminate := True;
  for i := 0 to 1000000000 do
    FloatToStr(i*1.31234);
end;

procedure TfrmMain.Button1Click(Sender: TObject);
var
  t1, t2, t3: TTaskThread;
begin
  t1 := TTaskThread.Create(True);
  t2 := TTaskThread.Create(True);
  t3 := TTaskThread.Create(True);
  t1.Start;
  t2.Start;
  t3.Start;
end;

这是一个测试代码,其中CPU(通过性能监视器)最大达到25% (我有4个核心)。如果FloatToStr行被换成非字符串操作,例如Power(i,2),然后性能监视器将显示预期的75%使用率。
(是的,有更好的方法来衡量此问题,但我认为这足以解决这个问题)

This is a 'test code', where the CPU (via performance monitor) maxes out at 25% (I have 4 cores). If the FloatToStr line is swapped for a non string operation, e.g. Power(i, 2), then the performance monitor shows the expected 75% usage. (Yes there are better ways to measure this, but I think this is sufficient for the scope of this question)

我已经对这个问题进行了相当详尽的探讨。该问题的目的是以一种非常简单的形式提出问题的症结所在。

I have explored this issue fairly thoroughly. The purpose of the question was to put forth the crux of the issue in a very simple form.

我在询问使用FloatToStr方法时的局限性。并问是否存在实现的实现,它将更好地利用可用内核。

I am asking about limitations when using the FloatToStr method. And asking is there an implementation incarnation which will permit better usage of available cores.

谢谢。

推荐答案

如果您不能更改内存管理器(MM),唯一要做的就是避免在MM可能成为瓶颈的地方使用它。

If you can't change the memory manager (MM) the only thing to do is to avoid using it where MM could be a bottleneck.

关于浮点到字符串的转换(Disclamer:我用Delphi XE测试了下面的代码),而不是

As for float to string conversion (Disclamer: I tested the code below with Delphi XE) instead of

procedure Test1;
var
  i: integer;
  S: string;

begin
  for i := 0 to 10 do begin
    S:= FloatToStr(i*1.31234);
    Writeln(S);
  end;
end;

您可以使用

procedure Test2;
var
  i: integer;
  S: string;
  Value: Extended;

begin
  SetLength(S, 64);
  for i := 0 to 10 do begin
    Value:= i*1.31234;
    FillChar(PChar(S)^, 64, 0);
    FloatToText(PChar(S), Value, fvExtended, ffGeneral, 15, 0);
    Writeln(S);
  end;
end;

产生相同的结果,但不在循环内分配内存。

which produce the same result but does not allocate memory inside the loop.

这篇关于并行处理字符串Delphi的全部可用CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆