Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi .
i have one question
how can i measure time and memory that one program used?
it means i want to write a program that this program use 12MB memory and this program excecute in 1 second. i want to know how i can measure time and memory of this program.
please help me.
thanks alot.
Posted
Comments
Sergey Alexandrovich Kryukov 18-Mar-15 14:49pm    
The notion of time here is ambiguous. Time span (duration)? CPU time used for a particular process?
Also, "I want to write a program that this program use 12MB memory and this program excecute in 1 second" sound absurd. I cannot be possibly an application goal.
—SA
barneyman 18-Mar-15 22:19pm    
as SA notes, 'time' in a multithreaded/multicore OS is a thorny issue - your program could appear to run for 5 seconds, but did it use the CPU for all that time (probably not), if it used 10CPUs for 20% of that time, did it run for 10 seconds? How much of *your* runtime was you, and not the Exec running/sleeping/spinning

Similarly with memory - do you want to know how much your code 'used', or how much was allocated to you by the OS, do you mean real memory, or virtual memory - do you wish to include your static/code/stack in that

Simply calling HeapSize and QueryPerformanceCounter (in windows) will give you results that are simultaneously right, and wrong :)
chandanadhikari 19-Mar-15 8:16am    
why not use some already available profiler

1 solution

I can answer at least the question on the process times.

Operation systems provide functions to get the times a process has spend. But these functions are thread specific. So you must get the time for each thread. This is usually done when a thread will terminate.

With Windows, use GetThreadTimes() passing the handle of the thread.

With Linux call clock_gettime() with CLOCK_THREAD_CPUTIME_ID from within the thread. To get the total thread run time, store the start time when the process is started to use it later for the calculation.

Here an example for Windows:
void PrintThreadTimes(HANDLE hThread, LPCSTR lpszName)
{
    FILETIME ftCreate, ftExit, ftKernel, ftUser;
    SYSTEM_INFO si;
    ::GetSystemInfo(&si);
    if (::GetThreadTimes(hThread, &ftCreate, &ftExit, &ftKernel, &ftUser))
    {
        unsigned long long nCreate, nUser, nKernel, nExit;
        SYSTEMTIME st1, st2;
        DWORD dwExitCode;
        // ftExit is undefined when thread still running
        if (STILL_ACTIVE == ::GetExitCodeThread(hThread, &dwExitCode))
            ::GetSystemTimeAsFileTime(&ftExit);
        nUser = (ftUser.dwHighDateTime << 32ULL) + ftUser.dwLowDateTime;
        nKernel = (ftKernel.dwHighDateTime << 32ULL) + ftKernel.dwLowDateTime;
        nExit = (ftExit.dwHighDateTime << 32ULL) + ftExit.dwLowDateTime;
        nCreate = (ftCreate.dwHighDateTime << 32ULL) + ftCreate.dwLowDateTime;
        double fCpuUsage = 100.0 * (nKernel + nUser) / (nExit - nCreate);
        double fAvCpuUsage = fCpuUsage / si.dwNumberOfProcessors;
        ::FileTimeToSystemTime(&ftKernel, &st1);
        ::FileTimeToSystemTime(&ftUser, &st2);
        printf("%s thread times: kernel %02u:%02u:%02u.%03u, user %02u:%02u:%02u.%03u, CPU usage %.1f %%\n",
            lpszName, 
            st1.wHour, st1.wMinute, st1.wSecond, st1.wMilliseconds,
            st2.wHour, st2.wMinute, st2.wSecond, st2.wMilliseconds,
            fAvCpuUsage);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900