how can I bring to my C program variables the following values:
如何为我的C程序变量引入以下值:
-
CPU used for the execution of the program, ie, how much processor is spent on that same program.
用于执行程序的CPU(即在同一程序上花费多少处理器)。
-
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
提前谢谢大家
2 个解决方案
#1
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".
可执行程序的编译器警告是在您的程序建立之后才可用的信息。因此,我认为不可能将这些信息静态地打包到“C程序变量”中。
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).
您可以将这些数据保存到一个文件中作为额外的构建步骤。例如,编写一个执行编译器并读取其输出的程序。这个程序要么将数据保存到一个文件中,要么给它一个链接器,并告诉它把它作为ressource打包(但是你会丢失链接警告)。
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)
大小、cpu使用率和运行时间是程序在运行时可以获取的信息。您可以通过C库(fopen等)轻松获得文件的大小。可以通过在应用程序启动时启动计时器来获得运行时间,并在退出之前读取计时器以获得总运行时间。我认为,对于cpu的使用,你必须询问操作系统(快速搜索谷歌windows就得到了这个)
#2
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)
只适用于windows:这里有一些代码可以用来获得您想要的一些东西。这个实现只返回PeakWorkingSize,但是我已经包含了包含所有可以获得的值的结构体的注释副本,并进行了一些修改。如果包含psapi,这将在ANSI C中编译和构建。lib(部分windows SDK安装,可在此免费下载)
#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;
}
#1
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".
可执行程序的编译器警告是在您的程序建立之后才可用的信息。因此,我认为不可能将这些信息静态地打包到“C程序变量”中。
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).
您可以将这些数据保存到一个文件中作为额外的构建步骤。例如,编写一个执行编译器并读取其输出的程序。这个程序要么将数据保存到一个文件中,要么给它一个链接器,并告诉它把它作为ressource打包(但是你会丢失链接警告)。
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)
大小、cpu使用率和运行时间是程序在运行时可以获取的信息。您可以通过C库(fopen等)轻松获得文件的大小。可以通过在应用程序启动时启动计时器来获得运行时间,并在退出之前读取计时器以获得总运行时间。我认为,对于cpu的使用,你必须询问操作系统(快速搜索谷歌windows就得到了这个)
#2
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)
只适用于windows:这里有一些代码可以用来获得您想要的一些东西。这个实现只返回PeakWorkingSize,但是我已经包含了包含所有可以获得的值的结构体的注释副本,并进行了一些修改。如果包含psapi,这将在ANSI C中编译和构建。lib(部分windows SDK安装,可在此免费下载)
#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;
}