小编Bru*_*dle的帖子

C++:跟踪调用函数的时间

将计数器作为参数传递给函数时,跟踪调用函数的次数很容易.从被调用函数返回一个时也很容易.但是,我不想走那条路.这背后的原因是因为它似乎是糟糕的编程(让函数知道太多信息).有没有更好的方法来跟踪调用此函数的次数?

我只是在寻找可以学习的概念.提供代码示例不是必需的,但可能会有所帮助.

编辑:我实际上并不是在寻找分析工具.让我添加一些代码来解释我的观点.因为funcCounter的范围以main结尾,所以我无法从myFunction返回一个将增加funcCounter的变量.我可以从myFunction返回1,然后以这种方式递增funcCounter,但这似乎不是很好的编程.还有另一种方法吗?

int main()
{
int funcCounter = 0;
char *mystring = "This is a silly function.";
myFunction(mystring);
cout << "Times function is called: " << funcCounter << endl;
    return 0;
}

void myFunction(char *mystring)
{
cout << mystring << endl;
}
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
2万
查看次数

如何获得应用程序启动所需的时间?

我正在编写一个C#应用程序,它需要能够告诉某个应用程序打开需要多长时间.我使用秒表类作为我的计时器.开始时间很简单,因为我完全使用运行.exe的调用来设置它.问题是找出程序打开时的时间.我唯一能想到的就是使用一个PerformanceCounter对象并使用while循环检查CPU%是否小于某个数字.

目前我正在使用PerformanceCounter,但我没有运气显示CPU%(它总是显示0).我的猜测是,应用程序打开的速度比PerformanceCounter可以检查CPU%的速度快,或者PerformanceCounter没有看到进程名称,因为我调用的速度太快(我非常怀疑后者是因为如果发生这种情况,我想我会得到错误).

还有其他方法可以解决这个问题吗?有没有什么我可能做错了,一直给我一个0%的CPU?我不是在申请之外寻找外部工具.以下是我的代码示例:

otherApp = new otherApplication.Application();
PerformanceCounter cpuCounter = new PerformanceCounter("Process",
"% Processor Time", "otherApplication");
//This next line is to check if PerformanceCounter object is working
//MessageBox.Show(cpuCounter.NextValue().ToString());
stopwatch.Start();
while (cpuCounter.NextValue() > 10)
{
}
stopwatch.Stop();
Run Code Online (Sandbox Code Playgroud)

编辑:更改代码以说出otherApp和otherApplication而不是myApp和myApplication,以便更容易理解.

c# time startup

5
推荐指数
1
解决办法
2336
查看次数

C++:重载数学运算符

我正在寻找重载,比方说,添加运算符并让它添加两个相同类的对象.当我在头文件中的类声明中声明这个"operator +"原型函数时,我将两个对象作为参数传递.我得到一个编译器错误,说"二进制'运算符+'有太多的参数".我在网上搜索一个答案,发现在编译出来的头文件中声明了一个内联函数.我想知道我做错了什么,或者我在这里错过了什么.这是我在头文件中使用的代码.

class Person
{
private:
    int age;
    double weight;
public:
    Person::Person();                           //default constructor       
    Person::~Person();                           //default desctructor
    Person operator+(Person r, Person i);
};
Run Code Online (Sandbox Code Playgroud)

这编译了我上面提到的错误.下面是编译好的代码.

class Person
{
private:
    int age;
    double weight;
public:
    Person::Person();                           //default constructor       
    Person::~Person();                           //default desctructor
};
inline Person operator+(Person r, Person i)
{
return Person(0,0); 
}
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

3
推荐指数
2
解决办法
1670
查看次数

标签 统计

c++ ×2

c# ×1

operator-overloading ×1

startup ×1

time ×1