C語言實現獲取內存信息並輸出的實例。本站提示廣大學習愛好者:(C語言實現獲取內存信息並輸出的實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C語言實現獲取內存信息並輸出的實例正文
C語言實現獲取內存信息並輸出的實例
實現實例代碼:
headfile.h
#include <stdio.h> #include <stdlib.h> #include <time.h> #define TRUE 1 #define FALSE 0 #define MAX 10000 typedef int KeyType; typedef int OtherType; typedef struct { KeyType key; OtherType other_data; }RecordType;
seek.cpp
#include "stdafx.h" #include "headfile.h" #include "windows.h" #include "conio.h " #include"WinBase.h" #include "Psapi.h" #pragma once #pragma message("Psapi.h --> Linking with Psapi.lib") #pragma comment(lib,"Psapi.lib") int Data[MAX]={0}; void produceData(int a[],int length) //給數組生成數據,用於隨即查找 { time_t t; srand(time(&t)); for (int i=0;i<length;i++) { a[i]=rand()%length; } } void printData(int a[],int length) //打印數字,到控制台,每五個換一行 { for (int i=0;i<length;i++) { printf("%8d",a[i]); if (0==i%5) { printf("\n"); } } } double showMemoryInfo() { double MemorySize; //單位MB HANDLE handle=GetCurrentProcess(); PROCESS_MEMORY_COUNTERS pmc; GetProcessMemoryInfo(handle,&pmc,sizeof(pmc)); MemorySize=pmc.WorkingSetSize/1024; printf("內存使用: %8lf \n",MemorySize); //WorkingSetSize The current working set size, in bytes. return MemorySize; } void writeRecordtime(unsigned rTime)//將程序結果運行時間寫入文件 { FILE *fpRecord=NULL; char *s="your programm running time is: "; char *c="ms "; if((fpRecord=fopen("record.txt","wt+"))==NULL) { printf("Cannot open file strike any key exit!"); getchar(); exit(1); } fprintf( fpRecord, "%s", s); fprintf( fpRecord, "%d", rTime); fprintf( fpRecord, "%s", c); fprintf( fpRecord, "\n"); fprintf( fpRecord, "your programm use %fMB size of memory!!!", showMemoryInfo()); fclose(fpRecord); } int _tmain(int argc, _TCHAR* argv[]) { produceData(Data,MAX); printData(Data,MAX); getchar(); return 0; }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!