多线程和性能 [英] Multithreading and performance

查看:74
本文介绍了多线程和性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有一个C#程序:

1)从SQL Server加载30000项数据

2)循环遍历所有项目并对数据进行一些广泛的计算

3)循环遍历所有项目Writs the resutls返回DB

这需要大约半小时,在步骤2和步骤1 + 3之间几乎相等。该程序在与SQL Server相同的机器上运行


所以我有1个CPU密集步骤和2个磁盘I / O密集步骤。

我之前从未使用过线程,但我一直在考虑使用线程来提高性能。一个典型的线程会加载一篇文章的数据,执行算术

并回写结果。这样我希望保持处理器计算一个项目,而

光盘操作正在为其他项目执行。

这是可行的/易于实现的,这会更快比我目前的节目?


问候,


-

Jurgen

解决方案

嗨Jurgen,


我会创建三个线程 - 每个步骤一个线程。

他们会像管道一样:

1.线程正在将消息加载到队列中

2.当新消息到达队列时,它将从计算队列中删除并且

计算完成。消息被放入回写队列。

3.当结果到达回写队列时,它从队列中删除并保存

到数据库

类似于那个。

-

Miha Markic [MVP C#]

RightHand .NET咨询&开发 www.rthand.com

博客: http://cs.rthand.com/blogs/blog_with_righthand/


" Jurgen" < jurgenDOTdebruyneATgmailDOTcom>在消息中写道

新闻:xn *************** @ news.microsoft.com ...

你好,

我有一个C#程序:
1)从SQL Server加载30000项数据
2)循环遍历所有项目并对
进行一些广泛的计算数据
3)循环遍历所有项目将结果返回到DB
这需要大约半小时,在步骤2和步骤1 + 3之间几乎相等。该程序在与SQL Server相同的机器上运行。
所以我有1个CPU密集步骤和2个磁盘I / O密集步骤。
我之前从未使用过线程,但是我一直在考虑使用
线程来提高性能。典型的线程会为单个文章加载数据,执行算法
并回写结果。这样我希望保持处理器计算物品的同时对其他物品执行光盘操作。
这是可行的/易于实现的,这将比我目前的更快<程序?

问候,

- / /> Jurgen



Definetly使用线程....但也想到使用队列'。

线程的数量我认为一个线程为evrry记录spwaned,即将

排队和重度计算....这是3000个线程.....你有to

测试它....这可能是一个非常错误的举动。


-------

问候,

C#,VB.NET,SQL SERVER,UML,设计模式面试问题书
http://www.geocities.com/dotnetinterviews/

我的访谈博客
http://spaces.msn.com/members/dotnetinterviews/




" Jurgen" < jurgenDOTdebruyneATgmailDOTcom>在消息中写道

新闻:xn *************** @ news.microsoft.com ...

你好,

我有一个C#程序:
1)从SQL Server加载30000项数据
2)循环遍历所有项目并对
进行一些广泛的计算数据3)循环遍历所有项目将结果返回到数据库
这需要大约半小时,在步骤2和
步骤1 + 3之间几乎相等。该程序在与SQL Server相同的机器上运行。
所以我有1个CPU密集步骤和2个磁盘I / O密集步骤。
我以前从未使用过线程,但我已经一直在考虑使用
线程来提高性能。典型的线程会加载单个文章的数据,
执行算术并回写结果。这样我希望保持处理器
计算一个项目,同时执行其他项目的光盘操作。
这是可行的/易于实现的,这将比我目前的
程序更快?
问候,

- Jurgen




Jurgen,


我同意其他海报,使用一个线程来读/写记录

(块中,不是一次全部)进入队列或从队列中取出。


当涉及到处理时,我会使用一个线程池,根据本地逻辑处理器的数量,在池中使用

个线程数

机器。这样,如果应用程序在MP盒上运行(并且使用多核

变得流行,那很可能)你可以利用它而不需要

重新编写应用程序。


您需要进行一些分析,以获得每个处理器的最佳

个线程数。我的第一直觉是两个,所以每个逻辑处理器上都会运行



Simon


Hello,

I''ve a C# program that:
1) loads data for 30000 items from SQL Server
2) loops through all the items and does some extensive calculaltions on the data
3) loops through all the items to Writs the resutls back to the DB
This takes about half an hour, almost equally divided between step 2 and step1+3. The program runs
on the same machine as the SQL Server.
So I have 1 CPU intensive step and 2 disc I/O intensive steps.
I''ve never used threads before, but I''ve been thinking about using threading to improve
performance. A typical thread would load the data for a single article, perform the arithmetics
and write back the results. This way I hope to keep the processor calculating for an item while
disc operations are being performed for other items.
IS this feasable/easy to achieve and will this be faster than my current program?

regards,

--
Jurgen

解决方案

Hi Jurgen,

I would create three threads - for each step one thread.
They would act like a pipeline:
1. thread is loading messages into a queue
2. when new message arrive in queue it is removed from calculation queue and
calculation is done. Message is put in writeback queue.
3. when result arrives in writeback queue it is removed from queue and saved
to database
Something like that.
--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Jurgen" <jurgenDOTdebruyneATgmailDOTcom> wrote in message
news:xn***************@news.microsoft.com...

Hello,

I''ve a C# program that:
1) loads data for 30000 items from SQL Server
2) loops through all the items and does some extensive calculaltions on
the data
3) loops through all the items to Writs the resutls back to the DB
This takes about half an hour, almost equally divided between step 2 and
step1+3. The program runs
on the same machine as the SQL Server.
So I have 1 CPU intensive step and 2 disc I/O intensive steps.
I''ve never used threads before, but I''ve been thinking about using
threading to improve
performance. A typical thread would load the data for a single article,
perform the arithmetics
and write back the results. This way I hope to keep the processor
calculating for an item while
disc operations are being performed for other items.
IS this feasable/easy to achieve and will this be faster than my current
program?

regards,

--
Jurgen



Definetly use threads .... but also think of using queue''s. Number of
threads i think one thread spwaned for evrry record i.e. for putting
to queue and heavy calculation....That is 3000 threads.....YOu have to
test it....It can be a very much wrong move.

-------
Regards ,
C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/
My Interview Blog
http://spaces.msn.com/members/dotnetinterviews/



"Jurgen" <jurgenDOTdebruyneATgmailDOTcom> wrote in message
news:xn***************@news.microsoft.com...

Hello,

I''ve a C# program that:
1) loads data for 30000 items from SQL Server
2) loops through all the items and does some extensive calculaltions on the data 3) loops through all the items to Writs the resutls back to the DB
This takes about half an hour, almost equally divided between step 2 and step1+3. The program runs on the same machine as the SQL Server.
So I have 1 CPU intensive step and 2 disc I/O intensive steps.
I''ve never used threads before, but I''ve been thinking about using threading to improve performance. A typical thread would load the data for a single article, perform the arithmetics and write back the results. This way I hope to keep the processor calculating for an item while disc operations are being performed for other items.
IS this feasable/easy to achieve and will this be faster than my current program?
regards,

--
Jurgen



Jurgen,

I would agree with the other posters, use one thread to read/write records
(in blocks, not all at once) into/from a queue.

When it comes to the processing, I''d use a thread pool, with the number of
threads in the pool based on the number of logical processors on the local
machine. That way, if the app is run on an MP box (and with multicore
getting popular, that''s pretty likely) you can take advantage of it without
having to re-write the app.

You''d need to do some profiling to come up with the optimal number of
threads per processor. My first instinct would be two, so that there''s
always be something running on each logical processor.

Simon


这篇关于多线程和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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