首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何实现内存和CPU功能的使用?

如何实现内存和CPU功能的使用?
EN

Stack Overflow用户
提问于 2013-10-19 19:10:30
回答 2查看 1.5K关注 0票数 1

比方说,我有一个函数(或函数),它需要很长时间(墙壁时间)来执行,例如:

代码语言:javascript
运行
复制
#include "stdafx.h"
#include <math.h>
#include <windows.h>

void fun()
{
  long sum = 0L;
  for (long long i = 1; i < 10000000; i++){
        sum += log((double)i);
    }
 }

double cputimer()
{
    FILETIME createTime;
    FILETIME exitTime;
    FILETIME kernelTime;
    FILETIME userTime;


    if ( GetProcessTimes( GetCurrentProcess( ),
        &createTime, &exitTime, &kernelTime, &userTime ) != -1 )
    {
        SYSTEMTIME userSystemTime;
        if ( FileTimeToSystemTime( &userTime, &userSystemTime ) != -1 )
            return (double)userSystemTime.wHour * 3600.0 +
            (double)userSystemTime.wMinute * 60.0 +
            (double)userSystemTime.wSecond +
            (double)userSystemTime.wMilliseconds / 1000.0;
    }

}

int _tmain(int argc, _TCHAR* argv[])
{
    double start, stop;

    start = cputimer();
    fun();
    stop = cputimer();


    printf("Time taken: %f [seconds]\n", stop - start);

    return 0;
}

我想测量这个函数的CPU负载和这个函数调用所使用的RAM使用情况。这有可能吗?我该怎么做?我对Windows和Linux解决方案感兴趣。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-19 19:14:12

在POSIX上,您可以尝试使用类似于检查墙壁时间的方式使用getrusage。不确定窗户的事。

票数 2
EN

Stack Overflow用户

发布于 2013-10-19 19:14:08

windows有GetProcessTimes函数,可以为您提供CPU时间。也可以检查进程状态API

另外还有与平台无关的SIGAR

在Linux上您可以尝试使用地学

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19469962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档