- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在运行一些计时和效率测试时遇到了一些意外行为。我发现,如果我运行其他将所有系统 CPU 核心都锁定在 100% 的后台进程,我的程序实际上运行得更快。这是一个简化的示例程序:
#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void vadd(const float *u, const float *v, float *y, int n) {
int i;
for (i = 0; i < n; i++) {
y[i] = u[i] + v[i];
}
}
int main(int argc, char *argv[]) {
int i, its = 100000, n = 16384;
float *a, *b, *c;
clock_t start, end;
double cpu_time;
/* Make sure alignment is the same on each run. */
posix_memalign((void**)&a, 16, sizeof(float) * n);
posix_memalign((void**)&b, 16, sizeof(float) * n);
posix_memalign((void**)&c, 16, sizeof(float) * n);
/* Some arbitrary initialization */
for (i = 0; i < n; i++) {
a[i] = i;
b[i] = 4;
c[i] = 0;
}
/* Now the real work */
start = clock();
for (i = 0; i < its; i++) {
vadd(a, b, c, n);
}
end = clock();
cpu_time = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Done, cpu time: %f\n", cpu_time);
return 0;
}
我在(相当旧的)Pentium 4 @ 2.8GHz 上运行,超线程已打开,在/proc/cpuinfo 中显示为两个处理器。
系统相对空闲时的输出:
$ ./test
Done, cpu time: 11.450000
现在加载所有内核:
$ md5sum /dev/zero& ./test; killall md5sum
Done, cpu time: 8.930000
这个结果是一致的。我猜想我通过减少程序移动到另一个 CPU 的时间以某种方式提高了缓存效率,但这只是在黑暗中的一次尝试。谁能证实或反驳这一点?
第二个问题:我惊讶地发现每次运行时 cpu_time 的变化如此之大。上面使用的方法是taken right out of the GNU C manual ,并且我认为使用 clock()
可以保护我免受由于使用 CPU 的其他进程引起的时间波动的影响。显然,根据上述结果,情况并非如此。所以我的第二个问题是,clock()
方法真的是衡量性能的正确方法吗?
更新:我已经研究了有关 CPU 频率调整调节器的评论中的建议,我认为这不是这里发生的事情。我试图通过 watch grep\"cpu MHz\"/proc/cpuinfo
(建议 here )实时监控 CPU 速度,但我没有看到频率变化程序正在运行。我还应该在我的帖子中说明我正在运行一个相当旧的内核:2.6.25。
更新 2: 我开始使用下面的脚本来研究启动的 md5sum 进程的数量。即使我启动的进程多于逻辑 CPU,它也比单独运行更快。
更新 3: 如果我在 BIOS 中关闭超线程,这种奇怪的行为就会消失,并且运行总是需要大约 11 秒的 CPU 时间。看起来超线程与它有关。
更新 4: 我刚刚在双四核 Intel Xeon @ 2.5GHz 上运行了这个,没有看到任何上述奇怪的行为。这个“问题”可能非常特定于我的特定硬件设置。
#!/bin/bash
declare -i num=$1
for (( num; num; num-- )); do
md5sum /dev/zero &
done
time ./test
killall md5sum
--
$ ./run_test.sh 5
Done, cpu time: 9.070000
real 0m27.738s
user 0m9.021s
sys 0m0.052s
$ ./run_test.sh 2
Done, cpu time: 9.240000
real 0m15.297s
user 0m9.169s
sys 0m0.080s
$ ./run_test.sh 0
Done, cpu time: 11.040000
real 0m11.041s
user 0m11.041s
sys 0m0.004s
最佳答案
So my secondary question is, is the clock() method really the proper way to measure performance?
您可能更喜欢使用 clock_gettime(2)和 friend 。另请阅读 time(7)
详细信息可能是硬件(即 CPU + 主板)和内核特定的。
关于c - 为什么当我用其他任意工作使系统过载时我的程序运行得更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17926038/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!