线程会提高性能吗? [英] Will threading improve performance?

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

问题描述

我有一个这样设置的程序.这是一个.Net Framework 4控制台应用程序. 此程序用于从每个服务器上的每个日志文件(从上周开始)收集sc字节和cs字节.该程序已完成,但是需要很长时间才能运行.

I have a program set up like this. It is a .Net Framework 4 console app. This program is used to gather the sc-bytes and cs-bytes from every log file (from the previous week) on each server. This program is finished, but takes a very long time to run.

foreach (string server in servers)
            {

                foreach (string website in Directory.GetDirectories(server))
                {

                    foreach (string file in Directory.GetFiles(website))
                    {

我只是想知道是否可以使用诸如线程或PLINQ之类的方法来加快进程? 如果这样做可以提高性能,我不确定实现它的最佳方法,因为为每个日志文件(甚至每个网站)都拥有一个新线程似乎是不合逻辑的,因为拥有这么多线程显然不会提高性能.

I was just wondering if something like threading or PLINQ could be used to speed up the process? If this would improve performance, I'm not sure of the best way to implement it because it seems illogical to have a new thread for each log file (or even each website) because having that many threads would obviously not improve performance.

如果需要查看更多代码,请询问,但是该程序基本上读取最近7天内创建的每个文件的每一行,添加字节数,然后使用目录条目获取网站名称(来自ID),然后将每个网站的名称和总字节输出到文本文件(最终将是数据库)中. 我不需要任何实际代码,只需建议最佳方法(如果可能)以提高性能.

If you need to see more code, please ask, but this program basically reads each line of each file that was created within the last seven days, adds the bytes numbers, and then uses directory entries to get the website name (from the ID), and then outputs the name and total bytes for each website into a text file (which will eventually be a database instead). I don't need any actual code, just advice on the best way (if possible) to improve performance.

谢谢.

推荐答案

在这样的IO绑定任务(遍历目录和文件并读取它们)中,瓶颈是磁盘IO,而不是CPU.

In an IO bound task like this (iterating over directories and files and reading them), the bottleneck is disk IO, not CPU.

并行化(?)可能无法帮助加快速度,甚至可能会损害性能.

Parallelizing (?) this is not likely to help with speeding it up and might even hurt performance.

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

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