我有一个在Linux上运行的C++应用程序,我正在优化它.如何确定代码的哪些区域运行缓慢?
我正在编写一个小程序,必须对一个大型数组(最多400万个文本字符串)进行排序.似乎我在这方面做得很好,因为radixsort和mergesort的组合已经将原始q(uick)排序执行时间减少了不到一半.
执行时间是主要的一点,因为这是我用来对我的代码进行基准测试的.
我的问题是:
是否有更好的(即更可靠的)基准测试程序的方式,而不仅仅是执行的时间?它有点工作,但是如果运行两次,相同的程序(运行相同的后台进程)通常具有稍微不同的执行时间.
这有点挫败了检测小改进的目的.一些小的改进可能会增加一个很大的...
提前感谢任何输入!
结果:
我设法让gprof在Windows下工作(使用gcc和MinGW).与我的普通编译器(tcc)相比,gcc表现不佳(考虑执行时间),但它给了我很多洞察力.
访问const变量的速度是否比非const变量快?我想知道是否值得使用const更多作为优化程序的一步.
我熟悉多线程,我已经成功地用Java和Objective-C开发了许多多线程程序.但是在没有使用主线程的连接的情况下,我无法使用pthreads在C中实现以下内容:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_OF_THREADS 2
struct thread_data {
int start;
int end;
int *arr;
};
void print(int *ints, int n);
void *processArray(void *args);
int main(int argc, const char * argv[])
{
int numOfInts = 10;
int *ints = malloc(numOfInts * sizeof(int));
for (int i = 0; i < numOfInts; i++) {
ints[i] = i;
}
print(ints, numOfInts); // prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
pthread_t threads[NUM_OF_THREADS];
struct thread_data thread_data[NUM_OF_THREADS];
// …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已被问过几次,但是没有一个真的能帮助我,所以再问一遍.
我正在使用Windows XP并运行visual studio c ++ 2008.
我正在寻找的所有代码都使用time.h但我认为可能在这里没有正常工作,因为结果让我怀疑.
所以这就是我想要的.
star time = get time some how (this is my question)
my code
end time = get time some how (this is my question)
time passed = start time - end time
Run Code Online (Sandbox Code Playgroud) 我有一个函数可以生成10000个随机数并将它们写在一个文件中.
void generator(char filename[])
{
int i;
int n;
FILE* fp;
if((fp=fopen(filename,"w+"))==NULL)
{
printf("Fail creating file?");
}
srand((unsigned)time(NULL));
for(i=0;i<10000;i++)
{
n=rand()%10000;
fprintf(fp,"%d ",n);
}
fclose(fp);
}
Run Code Online (Sandbox Code Playgroud)
如何使用C/C++获取此函数的执行时间?
c ×5
c++ ×4
benchmarking ×1
integer ×1
optimization ×1
performance ×1
posix ×1
profiling ×1
pthreads ×1
sorting ×1
time ×1
unix ×1