雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C語(yǔ)言實(shí)現(xiàn)獲取內(nèi)存信息并輸出的實(shí)例

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊:

C語(yǔ)言實(shí)現(xiàn)獲取內(nèi)存信息并輸出的實(shí)例

實(shí)現(xiàn)實(shí)例代碼:

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ù)字,到控制臺(tái),每五個(gè)換一行 
{ 
  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)行時(shí)間寫入文件 
{ 
  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; 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

上一篇:C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中二分查找遞歸非遞歸實(shí)現(xiàn)并分析

欄    目:C語(yǔ)言

下一篇:C++ 中重載和運(yùn)算符重載加號(hào)實(shí)現(xiàn)矩陣相加實(shí)例代碼

本文標(biāo)題:C語(yǔ)言實(shí)現(xiàn)獲取內(nèi)存信息并輸出的實(shí)例

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1685.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有