我感兴趣的是在Windows中强制CPU缓存刷新(出于基准测试的原因,我想模拟从CPU缓存中没有数据开始),最好是基本的C实现或Win32调用.
是否有一种已知的方法可以通过系统调用来执行此操作,甚至可以像执行大型操作一样偷偷摸摸地执行此操作memcpy?
英特尔i686平台(P4及以上版本也可以).
我有这个简单的你好世界程序:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我像平常一样用 LLVM Clang(v15.0.1,从 Homebrew 构建,所以不是 Apple 的版本)编译了这个程序,然后运行并计时了输出。令我惊讶的是,程序第一次运行的时间比第二次长了近 10 倍,但接下来的 3 次执行运行速度要快得多。
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在 Intel Core i5 mac 上运行此程序,运行 macOS Big Sur v11.6.8。shell 是bash随 macOS 一起提供的。
我的代码中没有任何内容涉及时间,而且我认为没有任何内容可以缓存,所以我不确定为什么第一次执行运行得这么慢。我怀疑操作系统可能正在进行某种优化,但我不知道是什么/如何进行。造成运行时间如此巨大差异的原因是什么?
我在xeon服务器上运行基准测试,我重复执行2-3次.我想在重复运行时擦除L1和L2中的缓存内容.你能建议任何方法吗?
我用这个标题的意思是,在某些情况下,在构建整个程序之后,它的第一次执行将需要大约 25 秒才能开始(直到第一个 printf 显示在控制台上)。下一次执行几乎立即开始(应该如此)。添加/删除一个空格并再次编译,之后的第一次执行再次极其缓慢。
Weather 我从 IDE (Code::Blocks) 或文件资源管理器中运行它没有任何改变。
但这是“解决”问题的方法:
我写的程序有一个循环,它一直在等待用户输入:
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
printf("Welcome!\n");
bool Running=true;
do{
char input[256], command[64];
if(fgets(input, 256, stdin) == NULL || input[0]=='\n')
continue;
sscanf(input, "%s", command);
string command_cppstr(command);
if(command_cppstr == "help")
{
printf("\n");
printf("help - displays this list\n");
printf("exit / quit - exits this progam\n\n");
continue;
}
if(command_cppstr == "exit" || command_cppstr == "quit")
{
Running = false;
continue;
}
printf("Unrecognized command. Use command \"help\" for …Run Code Online (Sandbox Code Playgroud) c ×2
x86 ×2
benchmarking ×1
c++ ×1
caching ×1
clang ×1
codeblocks ×1
cpu ×1
cpu-cache ×1
loops ×1
macos ×1
performance ×1
windows ×1