C語言實現(xiàn)獲取內(nèi)存信息并輸出的實例
更新時間:2017年03月25日 14:33:28 投稿:lqh
這篇文章主要介紹了C語言實現(xiàn)獲取內(nèi)存信息并輸出的實例的相關(guān)資料,需要的朋友可以參考下
C語言實現(xiàn)獲取內(nèi)存信息并輸出的實例
實現(xiàn)實例代碼:
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) //給數(shù)組生成數(shù)據(jù),用于隨即查找 { time_t t; srand(time(&t)); for (int i=0;i<length;i++) { a[i]=rand()%length; } } void printData(int a[],int length) //打印數(shù)字,到控制臺,每五個換一行 { 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("內(nèi)存使用: %8lf \n",MemorySize); //WorkingSetSize The current working set size, in bytes. return MemorySize; } void writeRecordtime(unsigned rTime)//將程序結(jié)果運行時間寫入文件 { 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; }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C++實現(xiàn)LeetCode(17.電話號碼的字母組合)
這篇文章主要介紹了C++實現(xiàn)LeetCode(17.電話號碼的字母組合),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07C語言數(shù)據(jù)結(jié)構(gòu)二叉樹先序、中序、后序及層次四種遍歷
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)二叉樹先序、中序、后序及層次四種遍歷方式,具有一定的知識性參考價值,需要的小伙伴可以先看一下2022-02-02