我正在尝试使用 gprof 分析 C++ 函数,我对所花费的 %时间感兴趣。我跑了不止一次,由于某种原因,结果有很大差异。我不知道是什么原因造成的,我假设采样率或者我在其他帖子中读到 I/O 与此有关。那么有没有一种方法可以使其更加准确并以某种方式产生几乎恒定的结果呢?
我在想以下几点:
等待您的输入
问候
使用两者来分析一些C++数字运算代码,gprof并kcachegrind为对执行时间贡献最大的函数(取决于输入的50-80%)给出类似的结果,但对于10-30%之间的函数,这些工具都给出不同的结果.这是否意味着其中一个不可靠?你会怎么做?
我使用gprof和报告来描述我的代码,大多数,如果不是所有前20个左右的东西都是关于向量的
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls ms/call ms/call name
14.71 0.05 0.05 3870399 0.00 0.00 std::vector<bool, std::allocator<bool> >::size() const
11.76 0.09 0.04 10552897 0.00 0.00 std::_Bit_reference::_Bit_reference(unsigned long*, unsigned long)
11.76 0.13 0.04 7890323 0.00 0.00 std::_Bit_const_iterator::_Bit_const_iterator(std::_Bit_iterator const&)
5.88 0.15 0.02 10089215 0.00 0.00 std::_Bit_iterator::operator*() const
5.88 0.17 0.02 6083600 0.00 0.00 std::vector<bool, std::allocator<bool> >::operator[](unsigned int)
5.88 0.19 0.02 3912611 0.00 0.00 std::vector<bool, std::allocator<bool> >::end() const
5.88 0.21 0.02 …Run Code Online (Sandbox Code Playgroud) 我有简单的排序程序,我正在分析,以便有一个案例来研究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) 这就是我正在做的事情.我的应用程序在拖动时从用户获取点并实时显示填充多边形.
它基本上在MouseMove上添加了鼠标位置.这一点是一个USERPOINT并且具有更好的句柄,因为最终我会做得更好,这就是为什么我必须将它们转换为向量.
所以基本上是MousePos - > USERPOINT.USERPOINT被添加到a std::vector<USERPOINT>.然后在我的UpdateShape()函数中,我这样做:
DrawingPoints定义如下:
std::vector<std::vector<GLdouble>> DrawingPoints;
Contour[i].DrawingPoints.clear();
for(unsigned int x = 0; x < Contour[i].UserPoints.size() - 1; ++x)
SetCubicBezier(
Contour[i].UserPoints[x],
Contour[i].UserPoints[x + 1],
i);
Run Code Online (Sandbox Code Playgroud)
SetCubicBezier()目前看起来像这样:
void OGLSHAPE::SetCubicBezier(USERFPOINT &a,USERFPOINT &b, int ¤tcontour )
{
std::vector<GLdouble> temp(2);
if(a.RightHandle.x == a.UserPoint.x && a.RightHandle.y == a.UserPoint.y
&& b.LeftHandle.x == b.UserPoint.x && b.LeftHandle.y == b.UserPoint.y )
{
temp[0] = (GLdouble)a.UserPoint.x;
temp[1] = (GLdouble)a.UserPoint.y;
Contour[currentcontour].DrawingPoints.push_back(temp);
temp[0] = (GLdouble)b.UserPoint.x;
temp[1] = (GLdouble)b.UserPoint.y;
Contour[currentcontour].DrawingPoints.push_back(temp);
}
else
{
//do cubic bezier calculation
}
Run Code Online (Sandbox Code Playgroud)
因此,由于立方贝塞尔的原因,我需要将USERPOINTS设置为GlDouble …
正如标题所暗示的那样:假设我们有一个复杂的程序,我们想让它更快,但我们可以.我们能否以某种方式检测哪些循环或其结构的其他部分占用大部分时间来进行优化?
编辑:注意,重要的是假设软件非常复杂,我们不能逐个检查每个循环或其他结构,将定时器放在其中等.
我正在编写一个小游戏来教我自己的OpenGL渲染,因为它是我尚未解决的问题之一.我之前使用过SDL,同样的功能,虽然仍然表现不佳,但并没有像现在那样超越顶级.
基本上,我的游戏还没有太多进展,只是一些基本的动作和背景绘图.当我切换到OpenGL,它看起来好像是这样太快了.我的每秒帧数超过2000,此功能耗尽了大部分处理能力.
有趣的是,它的SDL版本中的程序使用了100%的CPU但运行顺畅,而OpenGL版本只使用了大约40% - 60%的CPU,但似乎对我的显卡征税,使我的整个桌面变得无法响应.坏.
它不是一个太复杂的功能,它根据玩家的X和Y坐标呈现1024x1024背景图块,以给出运动的印象,同时玩家图形本身保持锁定在中心.因为它是用于更大屏幕的小瓷砖,所以我必须多次渲染它以将瓷砖拼接在一起以获得完整的背景.下面代码中的两个for循环迭代12次,合并,所以我可以看到为什么当每秒调用2000次时这是无效的.
所以为了达到目的,这是邪恶的行为者:
void render_background(game_t *game)
{
int bgw;
int bgh;
int x, y;
glBindTexture(GL_TEXTURE_2D, game->art_background);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &bgw);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &bgh);
glBegin(GL_QUADS);
/*
* Start one background tile too early and end one too late
* so the player can not outrun the background
*/
for (x = -bgw; x < root->w + bgw; x += bgw)
{
for (y = -bgh; y < root->h + bgh; y += bgh)
{ …Run Code Online (Sandbox Code Playgroud) c++ ×5
profiling ×5
gprof ×4
c ×2
optimization ×2
vector ×2
boolean ×1
debugging ×1
g++ ×1
gdb ×1
kcachegrind ×1
opengl ×1
performance ×1
profiler ×1
sdl ×1