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é)果運(yùn)行時間寫入文件
{
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)統(tǒng)計代碼運(yùn)行時間計時器的簡單實例
這篇文章主要介紹了 C++實現(xiàn)統(tǒng)計代碼運(yùn)行時間計時器的簡單實例的相關(guān)資料,需要的朋友可以參考下2017-07-07
C++基礎(chǔ)入門教程(九):函數(shù)指針之回調(diào)
這篇文章主要介紹了C++基礎(chǔ)入門教程(九):函數(shù)指針之回調(diào),本文講解了函數(shù)的地址、聲明函數(shù)指針、歷史原因、typedef挽救復(fù)雜的函數(shù)指針等內(nèi)容,需要的朋友可以參考下2014-11-11
C++讀入"N,X,Y,Z"格式文本文件到Eigen3 Matrix
這篇文章主要介紹了C++讀入"N,X,Y,Z"格式文本文件到Eigen3 Matrix,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
C語言中typedef的用法以及#define區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于C語言中typedef用法以及#define區(qū)別的相關(guān)資料,typedef 是用來定義一種類型的新別名的,它不同于宏(#define),不是簡單的字符串替換。而#define只是簡單的字符串替換(原地擴(kuò)展),需要的朋友可以參考下2021-07-07
C++中一維數(shù)組與指針的關(guān)系詳細(xì)總結(jié)
以下是對C++中一維數(shù)組與指針的關(guān)系進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下2013-09-09
VC++ loadlibrary()加載三方dll失敗, 返回錯誤碼:126的解決方法
今天在編寫VC++ loadlibrary()加載三方dll是總是失敗,并且返回錯誤碼:126,這里就為大家分享一下具體的解決方法2021-03-03

