根据以下结果,使用%操作在两个数字之间生成均匀的随机整数几乎比使用的快3倍std::uniform_int_distribution:是否有充分的理由使用std::uniform_int_distribution?
码:
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define N 100000000
int main()
{
clock_t tic,toc;
for(int trials=0; trials<3; trials++)
{
cout<<"trial: "<<trials<<endl;
// uniform_int_distribution
{
int res = 0;
mt19937 gen(1);
uniform_int_distribution<int> dist(0,999);
tic = clock();
for(int i=0; i<N; i++)
{
int r = dist(gen);
res += r;
res %= 1000;
}
toc = clock();
cout << "uniform_int_distribution: "<<(float)(toc-tic)/CLOCKS_PER_SEC << endl;
cout<<res<<" …Run Code Online (Sandbox Code Playgroud) 以下是我正在尝试的代码:
echo "a b c" | perl -e 'print $ARGV[0]; print $ARGV[1]; print $_;' "abc" "def"
Run Code Online (Sandbox Code Playgroud)
此代码的输出是:
abcdef
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么"打印$ _;" 不像往常一样打印"ab c".有任何想法吗?
我有一个像这样的字符串:
1,2,4 0:5 1:10 3:14
Run Code Online (Sandbox Code Playgroud)
我要转换成
1,2,4 1:5 2:10 4:14
Run Code Online (Sandbox Code Playgroud)
只有":"之前的数字必须加1.
我试过了:
perl -w -e '$s="1,2,4 0:5 1:10 3:14";
$s =~ s/([0-9]*):/print(($1+1).":")/ge;
print("$s\n");'
Run Code Online (Sandbox Code Playgroud)
奇怪的回归
1:2:4:1,2,4 15 110 114
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来实现我的目标?
我想在Ubuntu中一次安装所有可用的perl模块.这样的事情可能吗?
我需要在不同的文件上多次执行相同的perl脚本.
为了简化这个过程,我试图将perl脚本保存为bash字符串,并在字符串上调用perl,如下面代码的"不工作"部分:
#!/bin/sh
# works
perl -e 'print 1;'
# doesn't work
S="'print 1;'"
perl -e $S
perl -e $S
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
1Can't find string terminator "'" anywhere before EOF at -e line 1.
Can't find string terminator "'" anywhere before EOF at -e line 1.
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我能以其他方式达到同样的效果吗?
根据http://arma.sourceforge.net/docs.html#part_c,Armadillo支持以下功能:
eig_sym
eig_gen
eigs_sym
eigs_gen
svd
svd_econ
Run Code Online (Sandbox Code Playgroud)
但似乎没有像"svds_econ"这样的函数,它在"稀疏"矩阵上运行并返回奇异值和向量.
有没有办法在Armadillo中实现这个功能?