Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading for Performance Improvement

I have never used threads--never thought my code would benefit. However, I think threading might improve performance of the following pseudo code:

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

Loading each web page takes the most time. Parsing for the quote is quite fast. I guess I could take, say, half the records for one thread and work the other half in a second thread.

like image 233
Max Williams Avatar asked Feb 12 '12 02:02

Max Williams


People also ask

Does threading improve performance?

Multithreaded programs can improve performance compared to traditional parallel programs that use multiple processes. Furthermore, improved performance can be obtained on multiprocessor systems using threads.

How can we increase performance of application using threading?

Stay organized with collections Save and categorize content based on your preferences. Making adept use of threads on Android can help you boost your app's performance.

Does multithreading improve CPU performance?

In order to increase the speed of the processor core without changing the frequency, you can use multithreading to have the CPU process several tasks at once. Or to be precise, you can have it process several threads at once. A thread is a sequence of programmed instructions that's part of a larger process.


1 Answers

In OmniThreadLibrary it is very simple to solve this problem with a multistage pipeline - first stage runs on multiple tasks and downloads web pages and second stage runs in one instance and stores data into the database. I have written a blog post documenting this solution some time ago.

The solution can be summed up with the following code (you would have to fill in some places in HttpGet and Inserter methods).

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;
like image 78
gabr Avatar answered Sep 24 '22 10:09

gabr