4

how can I bring to my C program variables the following values​​:

  • CPU used for the execution of the program, ie, how much processor is spent on that same program.

  • The execution time of the program, ie, how long it took to be completed.

  • The compiler warnings, ie, how can I put compiler warnings on string variables in my own program?

  • The size of my program to disk: the program spend my hard disk.

I find this very difficult to do and I do not know any way of doing it.

Thanks to all in advance

Telmo Vaz
  • 189
  • 1
  • 2
  • 11

2 Answers2

0

The compiler warnings of the executable is information that is only available after your program has been built. So I think it is not easily possible to pack this information statically to your "C program variables".

You could save this data to a file as an additional build step. For example write a program that executes the compiler and reads its output. This program would then either save the data to a file or give it the linker and tell it to pack it as ressource (but then you would be missing linker warnings).

Size, cpu usage and run time is information that can be fetched by the program at runtime. You can get the size of a file easily with the C library (fopen etc). Run time can be gotten by starting a timer at the startup of your application and right before exiting you read that timer to get the total run time. I think for cpu usage you have to ask the operating system (a quick google search for windows got me this)

Community
  • 1
  • 1
typ1232
  • 5,535
  • 6
  • 35
  • 51
0

[EDITED to include program execution time, filesize]

For windows only: here is some code that can be used to get some of what you want. This implementation returns only PeakWorkingSize, but I have included a commented copy of the struct containing all of the values you can obtain, with minor modifications. This will compile and build in ANSI C if you include the psapi.lib (part of windows SDK installation, freely down loadable here)

#include <windows.h>
#include <ansi_c.h>
#include <psapi.h>

time_t GetMemUsage(void);

int main(int argc, char *argv[])
{
    DWORD start, elapsed; // for program execution time
    size_t memory; //for cpu usage;
    DWORD  filesize=0; //for exe file size
    FILE *fp;
    char buf[260];
    int i;

    start = GetTickCount();

    sprintf(buf, ".\\%s", argv[0]);

    fp = fopen(buf, "r");

    filesize = GetFileSize(fp, NULL);

    for(i=0;i<1000000;i++); //so ticks will be more than zero

    memory = GetMemUsage();

    fclose(fp);

    elapsed = GetTickCount() - start; //note, possible rollover, 

    return 0;   
}

time_t GetMemUsage(void)
{
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;
    DWORD processID = GetCurrentProcessId();

    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                    PROCESS_VM_READ,
                                    FALSE, processID );
    GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc));
    CloseHandle(hProcess);

//  typedef struct _PROCESS_MEMORY_COUNTERS {
//      DWORD cb;
//      DWORD PageFaultCount;
//      SIZE_T PeakWorkingSetSize;
//      SIZE_T WorkingSetSize;
//      SIZE_T QuotaPeakPagedPoolUsage;                             
//      SIZE_T QuotaPagedPoolUsage;
//      SIZE_T QuotaPeakNonPagedPoolUsage;
//      SIZE_T QuotaNonPagedPoolUsage;
//      SIZE_T PagefileUsage;
//      SIZE_T PeakPagefileUsage;
//  } PROCESS_MEMORY_COUNTERS;

typedef PROCESS_MEMORY_COUNTERS *PPROCESS_MEMORY_COUNTERS;

    return pmc.PeakWorkingSetSize;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87