我正在尝试优化计算密集型算法,并且遇到了一些缓存问题.我有一个巨大的缓冲区,它偶尔会随机写入,并且在应用程序结束时只能读取一次.显然,写入缓冲区会产生大量的缓存未命中,并且还会污染之后需要再次进行计算的缓存.我试图使用非时间移动内在函数,但缓存未命中(由valgrind报告并由运行时测量支持)仍然会发生.但是,为了进一步研究非时间动作,我写了一个小测试程序,你可以在下面看到.顺序访问,大缓冲区,只写.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <smmintrin.h>
void tim(const char *name, void (*func)()) {
struct timespec t1, t2;
clock_gettime(CLOCK_REALTIME, &t1);
func();
clock_gettime(CLOCK_REALTIME, &t2);
printf("%s : %f s.\n", name, (t2.tv_sec - t1.tv_sec) + (float) (t2.tv_nsec - t1.tv_nsec) / 1000000000);
}
const int CACHE_LINE = 64;
const int FACTOR = 1024;
float *arr;
int length;
void func1() {
for(int i = 0; i < length; i++) {
arr[i] = 5.0f;
}
}
void func2() {
for(int i = 0; i …Run Code Online (Sandbox Code Playgroud) 我应该能够Kernel在每个对象上调用方法,并format定义方法Kernel.为什么用第三个例子method_missing调用Kernel?
class A
def method_missing(meth, *args, &block)
if meth == :foo
puts 'ok'
elsif meth == :format
puts 'ok'
end
end
end
a = A.new
a.foo # => ok
a.send(:foo) # => ok
a.format # => ok
a.send(:format) # => too few arguments (ArgumentError)
Run Code Online (Sandbox Code Playgroud)