我测试了gcc C++标准库的Mersenne twister实现.它优于线性同余生成器和C rand,这很可能是LCG.增强文档似乎也给出了类似的结果,但更有利于Mersenne twister.有谁能解释一下?
#include <cstdlib>
#include <iostream>
#include <chrono>
#include <random>
class Timer
{
private:
std::chrono::high_resolution_clock::time_point start_time;
std::chrono::high_resolution_clock::time_point stop_time;
public:
void start()
{
start_time = std::chrono::high_resolution_clock::now();
}
void stop()
{
stop_time = std::chrono::high_resolution_clock::now();
}
double measure()
{
using namespace std::chrono;
return duration_cast<microseconds>
(stop_time - start_time).count() / 1000000.0;
}
};
template<typename T>
class Random
{
private:
T generator;
public:
Random()
: generator
(std::chrono::high_resolution_clock::now().time_since_epoch().count())
{
}
int generate_integer(int begin, int end)
{
return std::uniform_int_distribution<int>(begin, end - 1)(generator); …Run Code Online (Sandbox Code Playgroud) 那么,标准无法保证inline函数实际内联; 必须使用宏才能获得100%的保证.无论inline关键字如何,编译器总是根据自己的规则决定哪个函数内联或不内联.
那么,inline当使用现代编译器(如最新版本的GCC)时,关键字何时会对编译器的作用产生什么影响?
所以在我的程序中有一个if-else分支,大约有30个if-else语句.这部分每秒运行超过100次,因此我将其视为优化的机会,并使用函数指针数组(实际上是平衡树映射)进行二进制搜索,而不是进行线性if-else条件检查.但它的速度比以前的速度快了约70%.
我做了一个简单的基准测试程序来测试这个问题,它也给出了类似的结果,if-else部分运行得更快,无论是否有编译器优化.
我还计算了完成的比较次数,正如预期的那样,进行二进制搜索的人比简单的if-else分支做了大约一半的比较.但它仍然慢了20%~30%.
我想知道我的计算时间浪费在哪里,为什么线性if-else比对数二进制搜索运行得更快?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
long long ifElseCount = 0;
long long binaryCount = 0;
int ifElseSearch(int i) {
++ifElseCount;
if (i == 0) {
return 0;
}
++ifElseCount;
if (i == 1) {
return 1;
}
++ifElseCount;
if (i == 2) {
return 2;
}
++ifElseCount;
if (i == 3) {
return 3;
}
++ifElseCount;
if (i == 4) {
return 4;
}
++ifElseCount;
if (i == 5) {
return 5;
}
++ifElseCount;
if …Run Code Online (Sandbox Code Playgroud) 我无法将类的类型成员作为模板参数传递.例如,在以下代码中:
std::array<int, 1> a;
std::array<typename a::value_type, 1> a2;
Run Code Online (Sandbox Code Playgroud)
不会编译.
这是什么原因?有解决方法吗?
我正在尝试理解Haskell中的monad系统.我之前编程实验的大约80%都在C语言中,但具有讽刺意味的是,Haskell的必要部分是最难理解的.列表操作和惰性评估更加清晰.无论如何我想让ghc接受这个代码.我知道代码完全没有意义.最明显的是,我传递一个Bool地方IO Bool的预期.但这不是唯一的问题.我知道这是一个愚蠢的问题,但请帮助我进一步理解Haskell语言.
import Control.Monad
while :: Monad m => m Bool -> m () -> m ()
while cond action = do
c <- cond
when c $ do
action
while cond action
main :: IO ()
main = do
i <- 0
while (i < 10) $ do
i <- i + 1
print i
Run Code Online (Sandbox Code Playgroud)
这是我最终做到的.我知道allocaArray没有必要,但使用起来非常有趣.Haskell真的没有限制,非常强大.
import Control.Monad
import Data.IORef
import Foreign.Ptr
import Foreign.Storable
import Foreign.Marshal.Array
while :: Monad m => m Bool …Run Code Online (Sandbox Code Playgroud) staticinline如果我的理解是正确的,那么C++中函数的局部变量保证就像是一个全局变量一样存在.
如果inline函数是模板,那么同样适用,编译器可以生成函数的多个版本吗?
见BlendingTable::create和BlendingTable::print.两者都具有相同形式的尾递归,但是虽然create将被优化为循环,但print不会导致堆栈溢出.
下来看一个修复,我从一个gcc开发人员的提示中得到了我的错误报告中的问题.
#include <cstdlib>
#include <iostream>
#include <memory>
#include <array>
#include <limits>
class System {
public:
template<typename T, typename... Ts>
static void print(const T& t, const Ts&... ts) {
std::cout << t << std::flush;
print(ts...);
}
static void print() {}
template<typename... Ts>
static void printLine(const Ts&... ts) {
print(ts..., '\n');
}
};
template<typename T, int dimension = 1>
class Array {
private:
std::unique_ptr<T[]> pointer;
std::array<int, dimension> sizes;
int realSize;
public:
Array() {} …Run Code Online (Sandbox Code Playgroud) c++ recursion gcc functional-programming tail-call-optimization
我正在尝试使用异常打印类型名称,但我的程序似乎甚至没有捕获异常,而是似乎调用默认终止函数.我错过了什么?
#include <cstdio>
#include <exception>
#include <typeinfo>
namespace Error
{
template<typename T>
class Blah : std::exception
{
virtual const char* what() const throw()
{
return typeid(T).name();
}
};
}
void blah() {
throw Error::Blah<int*********>();
}
int main()
{
try
{
blah();
}
catch (std::exception& e)
{
std::puts(e.what());
}
}
Run Code Online (Sandbox Code Playgroud) 虽然我可以写
int n[] {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
我不能写
int *m = new int[] {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
应该是
int *m = new int[3] {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
这是什么原因?