线程池程序在速度更快的服务器上运行速度要慢得多

2023-12-15

upd我现在认为我的问题的根源不是“线程”,因为我观察到程序的任何一点都变慢了。我认为当使用 2 个处理器时,我的程序执行速度会变慢,可能是因为两个处理器需要彼此“通信”。我需要做一些测试。我将尝试禁用其中一个处理器,看看会发生什么。

=====================================

我不确定这是否是 C# 问题,可能更多是关于硬件的问题,但我认为 C# 最合适。

我使用的是便宜的 DL120 服务器,我决定升级到更昂贵的 2 处理器 DL360p 服务器。出乎意料的是,我的 C# 程序在新服务器上运行速度慢了约 2 倍,而新服务器本应快几倍。

我处理过FAST大约 60 种仪器的数据。我为每个仪器创建了单独的任务,如下所示:

        BlockingCollection<OrderUpdate> updatesQuery;
        if (instrument2OrderUpdates.ContainsKey(instrument))
        {
            updatesQuery = instrument2OrderUpdates[instrument];
        } else
        {
            updatesQuery = new BlockingCollection<OrderUpdate>();
            instrument2OrderUpdates[instrument] = updatesQuery;
            ScheduleFastOrdersProcessing(updatesQuery);
        }
        orderUpdate.Checkpoint("updatesQuery.Add");
        updatesQuery.Add(orderUpdate);
    }

    private void ScheduleFastOrdersProcessing(BlockingCollection<OrderUpdate> updatesQuery)
    {
        Task.Factory.StartNew(() =>
        {
            Instrument instrument = null;
            OrderBook orderBook = null;
            int lastRptSeqNum = -1;
            while (!updatesQuery.IsCompleted)
            {
                OrderUpdate orderUpdate;
                try
                {
                    orderUpdate = updatesQuery.Take();
                } catch(InvalidOperationException e)
                {
                    Log.Push(LogItemType.Error, e.Message);
                    continue;
                }
                orderUpdate.Checkpoint("received from updatesQuery.Take()");
                ......................
                ...................... // long not interesting processing code
        }, TaskCreationOptions.LongRunning);

因为我有大约 60 个可以并行执行的任务,所以我预计 2 * E5-2640(24 个虚拟线程,12 个真实线程)应该比 1 * E3-1220(4 个真实线程)执行得更快。看来使用 DL360p 我在任务管理器中发现了 95 个线程。使用 DL120 我只有 55 个线程。

但 DL120G7 上的执行时间快了 2 (!!) 倍! E3-1220 的时钟速率比 E5-2640 好一点(3.1 GHz vs 2.5Ghz),但是我仍然希望我的代码在 2 * E5-2640 上运行得更快,因为它可以更好地并行,我绝对不期望它的工作速度慢了两倍!

惠普 DL120G7 E3-1220

任务管理器中约 50 个线程最佳 = 24 个平均约 80 微秒

 calling market.UpdateFastOrder = 23 updatesQuery.Add = 25 received from updatesQuery.Take() = 67 in orderbook = 80
 calling market.UpdateFastOrder = 30 updatesQuery.Add = 32 received from updatesQuery.Take() = 64 in orderbook = 73
 calling market.UpdateFastOrder = 31 updatesQuery.Add = 32 received from updatesQuery.Take() = 195 in orderbook = 204
 calling market.UpdateFastOrder = 31 updatesQuery.Add = 32 received from updatesQuery.Take() = 74 in orderbook = 86
 calling market.UpdateFastOrder = 18 updatesQuery.Add = 21 received from updatesQuery.Take() = 65 in orderbook = 78
 calling market.UpdateFastOrder = 29 updatesQuery.Add = 32 received from updatesQuery.Take() = 76 in orderbook = 88
 calling market.UpdateFastOrder = 30 updatesQuery.Add = 32 received from updatesQuery.Take() = 80 in orderbook = 92
 calling market.UpdateFastOrder = 20 updatesQuery.Add = 21 received from updatesQuery.Take() = 65 in orderbook = 78
 calling market.UpdateFastOrder = 21 updatesQuery.Add = 24 received from updatesQuery.Take() = 68 in orderbook = 81
 calling market.UpdateFastOrder = 12 updatesQuery.Add = 13 received from updatesQuery.Take() = 58 in orderbook = 72
 calling market.UpdateFastOrder = 22 updatesQuery.Add = 23 received from updatesQuery.Take() = 51 in orderbook = 59
 calling market.UpdateFastOrder = 16 updatesQuery.Add = 16 received from updatesQuery.Take() = 20 in orderbook = 24
 calling market.UpdateFastOrder = 28 updatesQuery.Add = 31 received from updatesQuery.Take() = 82 in orderbook = 94
 calling market.UpdateFastOrder = 18 updatesQuery.Add = 21 received from updatesQuery.Take() = 65 in orderbook = 77
 calling market.UpdateFastOrder = 29 updatesQuery.Add = 29 received from updatesQuery.Take() = 259 in orderbook = 264
 calling market.UpdateFastOrder = 49 updatesQuery.Add = 52 received from updatesQuery.Take() = 99 in orderbook = 113
 calling market.UpdateFastOrder = 22 updatesQuery.Add = 23 received from updatesQuery.Take() = 50 in orderbook = 60
 calling market.UpdateFastOrder = 29 updatesQuery.Add = 32 received from updatesQuery.Take() = 76 in orderbook = 88
 calling market.UpdateFastOrder = 16 updatesQuery.Add = 19 received from updatesQuery.Take() = 63 in orderbook = 75
 calling market.UpdateFastOrder = 27 updatesQuery.Add = 27 received from updatesQuery.Take() = 226 in orderbook = 231
 calling market.UpdateFastOrder = 15 updatesQuery.Add = 16 received from updatesQuery.Take() = 35 in orderbook = 42
 calling market.UpdateFastOrder = 18 updatesQuery.Add = 21 received from updatesQuery.Take() = 66 in orderbook = 78

惠普 DL360p G8 2 * E5-2640

任务管理器中约有 95 个线程;最佳 = 40 平均 ~ 150 微秒

 calling market.UpdateFastOrder = 62 updatesQuery.Add = 64 received from updatesQuery.Take() = 144 in orderbook = 205
 calling market.UpdateFastOrder = 27 updatesQuery.Add = 32 received from updatesQuery.Take() = 101 in orderbook = 154
 calling market.UpdateFastOrder = 45 updatesQuery.Add = 50 received from updatesQuery.Take() = 124 in orderbook = 187
 calling market.UpdateFastOrder = 46 updatesQuery.Add = 51 received from updatesQuery.Take() = 127 in orderbook = 162
 calling market.UpdateFastOrder = 63 updatesQuery.Add = 68 received from updatesQuery.Take() = 137 in orderbook = 174
 calling market.UpdateFastOrder = 53 updatesQuery.Add = 55 received from updatesQuery.Take() = 133 in orderbook = 171
 calling market.UpdateFastOrder = 44 updatesQuery.Add = 46 received from updatesQuery.Take() = 131 in orderbook = 158
 calling market.UpdateFastOrder = 37 updatesQuery.Add = 39 received from updatesQuery.Take() = 102 in orderbook = 140
 calling market.UpdateFastOrder = 45 updatesQuery.Add = 50 received from updatesQuery.Take() = 115 in orderbook = 154
 calling market.UpdateFastOrder = 50 updatesQuery.Add = 55 received from updatesQuery.Take() = 133 in orderbook = 160
 calling market.UpdateFastOrder = 26 updatesQuery.Add = 50 received from updatesQuery.Take() = 99 in orderbook = 111
 calling market.UpdateFastOrder = 14 updatesQuery.Add = 30 received from updatesQuery.Take() = 36 in orderbook = 40   <-- best one I can find among thousands

你能明白为什么我的程序在速度快几倍的服务器上运行速度慢两倍吗?也许我不应该创建 ~60 个任务?也许我应该告诉 .NET 不要使用 95 个线程,而是将其限制为 50 个甚至 24 个?这可能是 2 个处理器与 1 个处理器的配置问题?也许只是禁用 DL360P Gen8 上的一个处理器就能显着加快程序速度?

Added

  • 调用 market.UpdateFastOrder - 创建 orderUpdate 对象
  • updateQuery.Add - orderUpdate 被放入 BlockingCollection 中
  • 从 updateQuery.Take() 接收 - orderUpdate 从 BlockingCollection 中弹出
  • 在 orderbook 中 - orderUpdated 被解析并应用于 orderBook

仅仅因为您有一个可以处理更多线程的系统,这并不意味着所有线程都可以完全并行处理。

当我从四核 CPU 升级到 i7(虚拟 8 核)时,我注意到使用比核心更多的线程的设置会导致线程相互阻塞一段时间,从而导致系统整体速度减慢。

问题在于我的算法已经能够使用其线程正在运行的核心的全部处理时间,而等待线程仅处理大约 5% 到 10%,这导致主线程完成,但一些单线程仍然有完成他们所有的工作(再次花费相同的时间)。

仅当所有工作线程都完成时,线程池才会继续,因此完成之前的总时间将是其他线程未使用的处理器时间。

也许您只需要找到最佳的线程数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

线程池程序在速度更快的服务器上运行速度要慢得多 的相关文章

  • 使用Physics.Raycast 和Physics2D.Raycast 检测对象上的点击

    我的场景中有一个空的游戏对象 带有 2D 组件盒碰撞器 我将脚本附加到该游戏对象 void OnMouseDown Debug Log clic 但是当我点击我的游戏对象时 没有任何效果 你有什么想法 如何检测我的盒子碰撞器上的点击 使用光
  • 如何访问另一个窗体上的ListView控件

    当单击与 ListView 所在表单不同的表单中的按钮时 我试图填充 ListView 我在 Form1 中创建了一个方法以在 Form2 中使用 并将参数传递给 Form1 中的方法 然后填充 ListView 当我调试时 我得到了传递的
  • 存储来自其他程序的事件

    我想将其他应用程序的事件存储在我自己的应用程序中 事件示例 打开 最小化 Word 或打开文件时 这样的事可能吗 运行程序 http msdn microsoft com en us library ms813609 aspx and 打开
  • 使用 C 语言使用 strftime() 获取缩写时区

    我看过this https stackoverflow com questions 34408909 how to get abbreviated timezone and this https stackoverflow com ques
  • 获取 WPF 控件的所有附加事件处理程序

    我正在开发一个应用程序 在其中动态分配按钮的事件 现在的问题是 我希望获取按钮单击事件的所有事件 因为我希望删除以前的处理程序 我尝试将事件处理程序设置为 null 如下所示 Button Click null 但是我收到了一个无法分配 n
  • 关于在 Windows 上使用 WiFi Direct Api?

    我目前正在开发一个应用程序 我需要在其中创建链接 阅读 无线网络连接 在桌面应用程序 在 Windows 10 上 和平板电脑 Android 但无关紧要 之间 工作流程 按钮 gt 如果需要提升权限 gt 创建类似托管网络的 WiFi 网
  • 如何在 Linq 中获得左外连接?

    我的数据库中有两个表 如下所示 顾客 C ID city 1 Dhaka 2 New york 3 London 个人信息 P ID C ID Field value 1 1 First Name Nasir 2 1 Last Name U
  • 读写器的信号量解决方案:更新读取器计数和等待或发出读/写二进制信号量信号之间的顺序?

    从操作系统概念来看 在解决第一个读者 作者问题时 读者 进程共享以下数据结构 semaphore rw mutex 1 semaphore mutex 1 int read count 0 do wait rw mutex writing
  • 在一个字节中存储 4 个不同的值

    我有一个任务要做 但我不知道从哪里开始 我不期待也绝对不想要代码中的答案 我想要一些关于该怎么做的指导 因为我感到有点失落 将变量打包和解包到一个字节中 您需要在一个字节中存储 4 个不同的值 这些值为 NAME RANGE BITS en
  • C++:.bmp 到文件中的字节数组

    是的 我已经解决了与此相关的其他问题 但我发现它们没有太大帮助 他们提供了一些帮助 但我仍然有点困惑 所以这是我需要做的 我们有一个 132x65 的屏幕 我有一个 132x65 的 bmp 我想遍历 bmp 并将其分成小的 1x8 列以获
  • 如何使用 watin 中的 FileUploadDialogHandler 访问文件上传对话框

    我正在使用 IE8 和 watin 并尝试通过我的网页测试上传文件 我不能简单地使用 set 方法设置上传文件 例如 ie FileUpload Find ById someId Set C Desktop image jpg 因为上传文本
  • Python 队列 get()/task_done() 问题

    我的消费者端队列 m queue get queue task done
  • 将 log4net 与 Autofac 结合使用

    我正在尝试将 log4net 与 Autofac 一起使用 我粘贴了这段代码http autofac readthedocs org en latest examples log4net html http autofac readthed
  • std::async 与重载函数

    可能的重复 std bind 重载解析 https stackoverflow com questions 4159487 stdbind overload resolution 考虑以下 C 示例 class A public int f
  • 如何对 Web Api 操作进行后调用?

    我创建了一个 Web API 操作 如下所示 HttpPost public void Load string siteName string providerName UserDetails userDetails implementat
  • gcc 的配置选项如何确定默认枚举大小(短或非短)?

    我尝试了一些 gcc 编译器来查看默认枚举大小是否很短 至少一个字节 强制使用 fshort enums 或无短 至少 4 个字节 强制使用 fno short enums user host echo Static assert 4 si
  • C++ 密码屏蔽

    我正在编写一个代码来接收密码输入 下面是我的代码 程序运行良好 但问题是除了数字和字母字符之外的其他键也被读取 例如删除 插入等 我知道如何避免它吗 特q string pw char c while c 13 Loop until Ent
  • Server.MapPath - 给定的物理路径,预期的虚拟路径

    我正在使用这行代码 var files Directory GetFiles Server MapPath E ftproot sales 在文件夹中查找文件 但是我收到错误消息说 给定物理路径但虚拟路径 预期的 我对在 C 中使用 Sys
  • 有没有办法强制显示工具提示?

    我有一个验证字段的方法 如果无法验证 该字段将被清除并标记为红色 我还希望在框上方弹出一个工具提示 并向用户显示该值无效的消息 有没有办法做到这一点 并且可以控制工具提示显示的时间 我怎样才能让它自己弹出而不是鼠标悬停时弹出 If the
  • memset 未填充数组

    u32 iterations 5 u32 ecx u32 malloc sizeof u32 iterations memset ecx 0xBAADF00D sizeof u32 iterations printf 8X n ecx 0

随机推荐

  • 求三角形三点的圆心[不使用指南针]

    我正在努力寻找外心给定三角形的三点 注意 所有这三个点都具有 X Y 和 Z 坐标意味着点是 3D 的 我知道外心是右平分线的交点 但为此我必须找到每条边的中点 然后是右平分线 然后是该点的交点 这个过程很长并且会出错 有没有什么公式它只是
  • 列出 DataGrid 中的目录文件

    我搜索了很多主题 但找不到有关使用 WPF 的答案DataGrid列出目录中的文件名内容 我能够输出内容ListBox但不知道如何将项目添加到Column in DataGrid 这适用于ListBox string path C obje
  • ASP MVC3 数据库优先

    我使用ASP MVC3应用程序的实体框架 起初我使用代码优先的方法 我创建了类并使用属性来验证数据字段 public class Person public int ID get set Required ErrorMessage Name
  • 以二进制方式将utf16写入文件

    我正在尝试以二进制模式使用 ofstream 将 wstring 写入文件 但我认为我做错了 这是我尝试过的 ofstream outFile test txt std ios out std ios binary wstring hell
  • 如何在vb代码内的DataTable上的linq中正确执行“group by”?

    为什么以下 group by 子句不起作用 最初的问题是如何在 LINQ 内的 vb 代码 dot net v4 0 中使用 DataTable 和组上的总和执行组 这是示例 但它没有产生所需的输出 它返回 2 行而不是 1 行 Dim t
  • 如何更新 Python 包?

    我正在运行 Ubuntu 9 10 并安装了一个名为 M2Crypto 的软件包 版本为 0 19 1 我需要下载 构建并安装最新版本的 M2Crypto 软件包 0 20 2 0 19 1 软件包在多个位置都有文件 包括 usr shar
  • java中有没有命令可以让程序回到循环的开头

    我正在尝试用java制作一款打字冒险类游戏 但是我需要一个至少类似于标题中的命令 这是代码 import java util Scanner public class MyFirstGameInJava public static void
  • 为什么我会收到此 Oracle 连接错误? ORA-12519,TNS:未找到适当的服务处理程序

    我正在使用 GlassFish Hibernate Spring 用于事务管理 和 Oracle 11g 构建一个 JSF2 应用程序 应用程序运行一段时间后 应用程序无法查询数据库 并且我在 GlassFish 日志中收到以下错误 原因
  • 嵌入 YouTube 视频 - 拒绝在框架中显示,因为它将“X-Frame-Options”设置为“SAMEORIGIN”[重复]

    这个问题在这里已经有答案了 我正在尝试向我的 Django 页面提供从其他地方获得的一些资源 在 feed 中 我有 YouTube 视频 其 URL 如下 https www youtube com watch v A6XUVjK9W4o
  • graph.facebook.com - 传输安全块

    错误 传输安全已阻止明文 HTTP http 资源加载 因为它不安全 可以通过应用程序的 Info plist 文件配置临时例外 当尝试访问 graph facebook com 等时 我已经把 NS允许任意负载在 plist 上 但它不断
  • 添加约束以防止 SQL 更新触发器中出现重复

    我们有一个用户表 每个用户都有一个唯一的电子邮件和用户名 我们尝试在代码中执行此操作 但我们希望确保用户永远不会使用相同的电子邮件用户名插入 或更新 到数据库中 我添加了一个BEFORE INSERT防止插入重复用户的触发器 CREATE
  • 使用英特尔编译器构建 Boost 库

    除了 Visual Studio 2012 之外 我还在 32 位 Windows 7 计算机上安装了 Intel Parallel Studio XE 2013 我尝试按照 Intel 编译器中的说明构建 Boost 1 53link 我
  • 在oracle中只检索表的第二行?

    谁能帮忙 如何从oracle表中准确检索第二行 由于表中的行本质上是无序的 因此 第一个 和 第二个 的概念要求您指定某种强制顺序的方法 即 ORDER BY 子句 最简单的方法是使用解析函数 SELECT FROM SELECT a ro
  • 如何在 SQL Server 中传递两个 sql 表作为 r 代码的输入参数

    我正在 SQL Server 中运行 r 代码 我在 SQL Server 数据库中有两个表 我想将它们作为输入数据集传递到 R 代码中 我可以用 input data 1传递输入数据集之一 我怎样才能通过另一张桌子 我读自一个 MSDN
  • 减少多个 box-shadow 参数的 mixin 问题

    我正在一个项目中工作 我必须减少使用 就我个人而言 我总是使用手写笔 但我不能使用这个项目 所以我有下一个问题 我怎样才能用更少的手写笔做到这一点 问题在于参数的数量 在手写笔中 box shadow webkit box shadow a
  • C++ 中新内存的范围

    当我尝试执行以下操作时 出现错误 提示我正在尝试读取或写入受保护的内存 void func1 int ptr int ptr b new int 5 ptr ptr b void main int ptr a func1 ptr a del
  • NoClassDefFoundError Android 与 ActionBarActivity

    我目前有一个ActionBarActivity它总是返回一个NoClassDefFoundError 我读到这可能是 ADT 的问题 但我不能确定 因此我提出了问题 我已经从 Android 示例中导入了 ActionBar 示例 andr
  • C++ 从向量中删除对象[重复]

    这个问题在这里已经有答案了 我想从向量中删除一个元素 例如 object that is in the vector MyClass obj vector looks as so vector
  • R:2个不同包中的2个同名函数

    我需要加载到 R 包 tseries 和 chron 两者都有一个名为 is weekend 的函数 我的环境中始终具有我加载的第二个包中的函数 我如何才能始终访问 chron 的功能 您可能已经注意到加载包的顺序会有所不同 即最后加载的包
  • 线程池程序在速度更快的服务器上运行速度要慢得多

    upd我现在认为我的问题的根源不是 线程 因为我观察到程序的任何一点都变慢了 我认为当使用 2 个处理器时 我的程序执行速度会变慢 可能是因为两个处理器需要彼此 通信 我需要做一些测试 我将尝试禁用其中一个处理器 看看会发生什么 我不确定这