我试图确定std::atomic
在我的系统(八核x64)上无条件内存写入的开销.这是我的基准程序:
#include <atomic>
#include <iostream>
#include <omp.h>
int main() {
std::atomic_int foo(0); // VERSION 1
//volatile int foo = 0; // VERSION 2
#pragma omp parallel
for (unsigned int i = 0; i < 10000000; ++i) {
foo.store(i, std::memory_order_relaxed); // VERSION 1
//foo = i; // VERSION 2
}
std::cout << foo << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
该程序将按原样进行基准测试std::atomic_int
,并对标记VERSION 1
的行进行注释并取消注释标记的VERSION 2
行将volatile int
在其位置进行测试.即使不同步,两个程序的输出也应为10000000 - 1.
这是我的命令行:
g++ -O2 -std=c++11 -fopenmp test.c++
Run Code Online (Sandbox Code Playgroud)
atomic_int
在我的系统上使用的版本需要2到3秒,而使用的版本volatile …
该计划格式不正确:
struct X { int i; };
int main() {
(X { }).i = 1;
}
Run Code Online (Sandbox Code Playgroud)
i
,临时的子对象X { }
,不能用作左值,因为它 X { }
是一个右值.
但是,这默默地编译了GCC 5.2.1和-Wall
:
using Y = int[10];
int main() {
(Y { })[0] = 1;
}
Run Code Online (Sandbox Code Playgroud)
如果编译器是正确的,那么这次,(Y { })
作为子对象的第0个元素(Y { })
可以被视为左值.
我的问题是:
作为haskell的新手,我想知道更有效的方法是:
mapmulti :: (a -> [b]) -> [a] -> [b]
mapmulti fn list = concat $ map fn list
Run Code Online (Sandbox Code Playgroud)
我的列表将长达数千到数百万个元素,创建一个N列表列表只是为了将它们连接在一起似乎是一种耻辱.
或者并且优选地,是否存在一些更通用(可能是monadic)的方式让haskell从函数的结果中逐步创建列表,以便逐步构建列表而不创建O(n)
要评估的大小的递归表达式树?