线程的性能改进

Max*_*ams 6 delphi multithreading

我从未使用过线程 - 从未想过我的代码会受益.但是,我认为线程可能会提高以下伪代码的性能:

Loop through table of records containing security symbol field and a quote field
    Load a web page (containing a security quote for a symbol) into a string variable
    Parse the string for the quote
    Save the quote in the table
    Get next record
end loop
Run Code Online (Sandbox Code Playgroud)

加载每个网页需要花费最多的时间.解析报价非常快.我想我可以拿一个线程的一半记录,然后在另一个线程中处理另一半.

gab*_*abr 4

在OmniThreadLibrary中,使用多级管道解决这个问题非常简单 - 第一阶段运行多个任务并下载网页,第二阶段运行在一个实例中并将数据存储到数据库中。我前段时间写了一篇博客文章记录了这个解决方案。

解决方案可以用以下代码总结(您必须填写 HttpGet 和 Inserter 方法中的某些位置)。

uses
  OtlCommon,
  OtlCollections,
  OtlParallel;

function HttpGet(url: string; var page: string): boolean;
begin
  // retrieve page contents from the url; return False if page is not accessible
end;

procedure Retriever(const input: TOmniValue; var output: TOmniValue);
var
  pageContents: string;
begin
  if HttpGet(input.AsString, pageContents) then
    output := TPage.Create(input.AsString, pageContents);
end;

procedure Inserter(const input, output: IOmniBlockingCollection);
var
  page   : TOmniValue;
  pageObj: TPage;
begin
  // connect to database
  for page in input do begin
    pageObj := TPage(page.AsObject);
    // insert pageObj into database
    FreeAndNil(pageObj);
  end;
  // close database connection
end;

procedure ParallelWebRetriever;
var
  pipeline: IOmniPipeline;
  s       : string;
  urlList : TStringList;
begin
  // set up pipeline
  pipeline := Parallel.Pipeline
    .Stage(Retriever).NumTasks(Environment.Process.Affinity.Count * 2)
    .Stage(Inserter)
    .Run;
  // insert URLs to be retrieved
  for s in urlList do
    pipeline.Input.Add(s);
  pipeline.Input.CompleteAdding;
  // wait for pipeline to complete
  pipeline.WaitFor(INFINITE);
end;
Run Code Online (Sandbox Code Playgroud)