我想利用std::stable_sort.算法的复杂性表示为
O(N·log ^ 2(N)),其中N = std :: distance(first,last)cmp的应用.如果有额外的内存,则复杂度为O(N·log(N)).http://en.cppreference.com/w/cpp/algorithm/stable_sort
在我的申请中,存储器是至关重要的,因此,我宁愿std::stable_sort使用存储器优化的O(N·登录^ 2(N))的算法,而不是时间优化O(N·日志(N))的算法.我知道只有在安全的情况下才会选择时间优化版本(可用内存).但是,我的目标是对我的应用程序进行基准测试,因此,当内存非常重要时,需要在内存消耗最低时对算法进行基准测试.由于我的系统目前有足够的内存来分配缓冲区,它会自动选择O(N·log ^ 2(N))版本std::stable_sort.因此,我想主张std::stable_sort采用内存优化版本.这可能吗?
执行策略似乎是可以修改算法的参数,但是,它似乎只会改变并行度. http://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t
虽然选择并行或顺序版本实际上可能实际上分别选择O(N·log(N))或O(N·log ^ 2(N))版本,但这在网页上的任何地方都没有说明.
我想知道是否有人有任何断言这种选择的经验.如果是这样他们能提供任何建议吗?
当我用下面的代码编译并运行代码counter++或++counter替换时x,输出是相同的; 在这两种情况下,数字1 - 10:
for (int counter = 1; counter < 11; x)
{
std::cout << counter << endl;
}
Run Code Online (Sandbox Code Playgroud)
最初我认为++counter会增加1,然后返回新值,然后在循环头中评估布尔表达式.即,当使用counter = 1和使用时++counter,counter布尔表达式中的值为2.这似乎并非如此,因为两个输出都是相同的,而不是++counter像我预期的那样具有少一次迭代的版本.
在周围读取时,它分别在循环体的开始或结束处出现++counter并counter++递增counter1.在这种情况下,至少在概念上,这不是一个相同的行动吗?因为一旦循环超过第一次迭代,循环的结束和开始是相同的.
我唯一能看到这个有所作为的是第一次迭代,std::cout << counter << endl;如果counter++使用的话,应该输出1到控制台(因为在循环结束时将1添加到计数器).虽然std::cout << counter << endl;应该输出2到控制台如果++counter被使用(因为1被添加在循环的开始到计数器).
除了上面的问题,你能否准确地解释在for循环标题中评估三个动作的顺序,并准确地解释使用i++和时迭代发生的位置++i.
非常感谢!
我有简单的排序程序,我正在分析,以便有一个案例来研究gprof; 我后来计划分析一个更大的算法.
我编译-pg并运行./sort来生成gmon.out文件.但是,当我运行gprof ./sort gmon.out累积秒数和自秒时产生的值时,我认为不准确.
首先,跑步time(./sort)我得到:
real 0m14.352s
user 0m14.330s
sys 0m0.005s
Run Code Online (Sandbox Code Playgroud)
我的秒表准确无误.
但是,平面轮廓的gprof结果是:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
56.18 2.76 2.76 1 2.76 4.71 sort(std::vector<int, std::allocator<int> >&)
35.01 4.49 1.72 1870365596 0.00 0.00 std::vector<int, std::allocator<int> >::operator[](unsigned long)
8.96 4.93 0.44 100071 0.00 0.00 std::vector<int, std::allocator<int> >::size() const
0.00 4.93 0.00 50001 0.00 0.00 __gnu_cxx::new_allocator<int>::construct(int*, int const&)
0.00 4.93 …Run Code Online (Sandbox Code Playgroud) 我试图生成长度为4与四个字符的所有可能的DNA序列的列表A,T,C,G.总共有4 ^ 4(256)种不同的组合.我包括重复,这AAAA是允许的.itertools.combinations_with_replacement(iterable, r)
不过我看过,列表输出会根据输入字符串的顺序而改变
itertools.combinations_with_replacement('ATCG', 4) #diff results to...
itertools.combinations_with_replacement('ATGC', 4)
Run Code Online (Sandbox Code Playgroud)
因此,我试图itertools.combinations_with_replacement(iterable, r)与之合并itertools.permutations()
使得传递的输出itertools.permutations()来itertools.combinations_with_replacement().定义如下:
def allCombinations(s, strings):
perms = list(itertools.permutations(s, 4))
allCombos = []
for perm in perms:
combo = list(itertools.combinations_with_replacement(perm, 4))
allCombos.append(combo)
for combos in allCombos:
for tup in combos:
strings.append("".join(str(x) for x in tup))
Run Code Online (Sandbox Code Playgroud)
然而,运行allCombinations('ATCG', li)在哪里li = [],然后采取
list(set(li))仍然只进行136个独特的序列,而不是256.
必须有一个简单的方法来做到这一点,可能会产生一个电源设置,然后过滤4长度?
假设我想从文件读取所有行并将其存储为字符串列表.我可以用:
filename = "/path/to/the/file/data.txt"
fileBuf = [line.strip() for line in open(filename, "r")]
Run Code Online (Sandbox Code Playgroud)
我的问题是,因为文件句柄是未命名的,我该如何关闭它?它会自动关闭吗?
我有两个产生多个线程并打印hello <TID> world <TID>到stdout的程序。第一个在一个函数中打印它:
#include <omp.h>
#include <stdio.h>
int main() {
#pragma omp parallel
{
int ID = omp_get_thread_num();
#pragma omp critical
printf("hello %d world %d\n", ID, ID);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
二分之二:
#include <omp.h>
#include <stdio.h>
int main() {
#pragma omp parallel
{
int ID = omp_get_thread_num();
#pragma omp critical
printf("hello %d", ID);
printf("world %d\n", ID);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
只需对调用一次,程序printf()就不会出现乱码。但是,在第二个程序中,对的两次调用printf()使输出出现乱码。在第二个程序中,永远不会有乱码干扰字符串序列。从来没有这样的东西hhhhellloohello...。但是,不同的输出字符串之间存在乱码;可能性的排列:
hello 27hello 62hello 52hello 50hello 10world 62...
Run Code Online (Sandbox Code Playgroud)
举个例子。
我以为在并行化块结束之前#pragma …
zlib gzopen()返回一个压缩文件流.
gzFile data_file;
data_file = gzopen(filename.c_str(), "r");
Run Code Online (Sandbox Code Playgroud)
data_file压缩文件流在哪里.我可能从zlib docs中错过了这个.但是,这会解压缩打开的文件吗?或者是否gzopen()能够直接解析gzip压缩文件而无需解压缩?
提前致谢!
我遇到了以下声明,并且头脑冷静.
++(*q)->len;
Run Code Online (Sandbox Code Playgroud)
q似乎是一个连续的内存数组,从它的外观来看,似乎是一个指针数组structs; 掌握一个新的代码库,抱歉模糊不清.
使用上面的语句我们解引用q,获取指针的值,然后我们取消引用并访问该字段len,然后我们递增?在代码中,我想说的是,这++((*q)->len)将是显式执行.
或者我应该读这个,我们取消引用q然后递增值,然后我们取消引用并获得len字段,struct即q在增量之前的那个.再次,在代码中,我想说的是,这(++(*q))->len将是显式执行.
任何指导都会很棒
更新:我想第一个解析是有道理的++((*q)->len),就好像它是第二种情况,我们会得到它的价值,len然后不做任何事情.
我正在尝试对大量函数进行基准测试,并且我已经定义了宏来概括时间戳.我已经制作了一个头文件benchmark,如下:
#include <chrono>
#include <iostream>
#define START(x) xstart = std::chrono::steady_clock::now()
#define END(x) xend = std::chrono::steady_clock::now()
#define TIME(x) xtime = std::chrono::duration_cast<std::chrono::nanoseconds>(xend-xstart).count()
#define PRINT(x) std::cout << #x << "(), " << xtime << std::endl
Run Code Online (Sandbox Code Playgroud)
对于所有宏,x用函数名替换,不带参数括号.例如PRINT(foobar);等等.但是,我已经使用了多个函数名称相同的宏,因为我以为我可以代入x多次.即
START(foobar);
// later...
START(func);
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误:
xstart’ has a previous declaration as ‘std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration\<long int, std::ratio<1l, 1000000000l> > > xstart
Run Code Online (Sandbox Code Playgroud)
一旦我用它来定义一个函数,我似乎无法重用该变量.但是,我从未在PRINT中遇到此错误.那么,是因为我在声明变量吗?
我基本上试图想出一个快速的时间戳功能,所以欢迎任何其他关于如何快速实现这一点的建议.