怎么测量一个程序的启动时间?

时间肯定时测不准的。我们只能大致测量一个值。

哪些因素会影响一个程序的启动时间?

CPU的主频?操作系统的调度算法(进程的优先级),应用程序的大小(运行时所占用资源(内存,计算资源)的多少)等。

就程序的大小而言,打开MATLAB(至少几秒钟吧)这样的大型软件的时间明显比打开python IDLE的时间长很多。

有的软件不是可以测量电脑的开机时间吗?

怎么写一个程序去测量另外一个程序的启动时间呢?

首先要规则:程序的启动时间怎么定义?

我们这里就以发起进程启动的时刻起,到目标进程启动执行到main()函数的第一条语句处为止,算做一个程序的启动时间吧!

这里就姑且愚蠢地操作一波!

可以了解一下一个程序的代码是怎么从硬盘装入内存,然后等会操作系统的调度,活得CPU时间片进而运行的呢?

以C/C++程序而言,在执行main()函数之前是要做很多预处理工作的!

1.

C的源程序.cpp文件->编译预处理->编译->汇编->链接库函数->得到.exe文件

参考[1]https://blog.csdn.net/shixin_0125/article/details/78777554

以c程序为例,转换过程大致分为预处理,编译,汇编,链接四个步骤

2.C++程序进入main函数之前,退出main函数之后会做些什么?

[2]https://blog.csdn.net/xiongbixb2/article/details/49716963

总结: main函数执行之前的,主要就是初始化系统相关资源:

1.设置栈指针

2.初始化static静态和global全局变量,即data段的内容

3.将未初始化部分的赋初值:数值型short,int,long等为0,bool为FALSE,指针为NULL,等等,即.bss段的内容

4.运行全局构造器,估计是C++中构造函数之类的吧

5.将main函数的参数,argc,argv等传递给main函数,

然后才真正运行main函数

main 函数退出之后会执行相反的工作。

3.操作系统对进程的调度

[3]https://blog.csdn.net/chengonghao/article/details/50981214

这里是在Windows平台下,vs2013:

先了解两个函数:

GetTickCount()

#include <windows.h>

函数原型:

__drv_preferredFunction("GetTickCount64", 
"GetTickCount overflows roughly every 49 days. 
 Code that does not take that into account can loop indefinitely. 
 GetTickCount64 operates on 64 bit values and does not have that problem")
WINBASEAPI
DWORD
WINAPI
GetTickCount(
    VOID
    );

GetTickCount是一种函数。GetTickCount返回(retrieve)从操作系统启动所经过(elapsed)的毫秒数,它的返回值是DWORD。

2.system()函数,可以执行DOS命令。

system是一个C语言和C++下的函数。windows操作系统下system () 函数详解主要是在C语言中的应用,system函数需加头文件<stdlib.h>后方可调用。

system()在windows下的版本:

#include<stdlib.h>

函数名: system

功 能: 发出一个DOS命令

用 法: int system(char *command);

函数原型:

_CRTIMP int __cdecl system(_In_opt_z_ const char * _Command);

我们就用system("myApp.exe")打开当前路径下的myApp程序。

用下面的代码生成的TestStartTime2.exe程序启动app2.exe:将启动时间和截至时间输出到屏幕

注意TestStartTime2.exe和app2.exe放在一个文件夹下!

// TestStartTime2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<Windows.h>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	long startTime = GetTickCount();
	cout << "startTime=" << startTime << endl;
	system("app2.exe");

	system("pause");
	return 0;
}

// app2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<Windows.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	long t_end = GetTickCount();
	cout << "t_end=" << t_end << endl;

	system("pause");
	return 0;
}

输出示例:

startTime=116683140
t_end=116683171
请按任意键继续. . .

2.心血来潮:进程间通个讯吧,然后之间将时间差计算出来!

进程间通信有N种方式,选哪一种呢?就共享内存吧,简单,快速!

参考:[1]C++中进程间相互通信的十一种方法 https://www.cnblogs.com/swunield/articles/3893250.html

那么Windows下的C++程序怎么实现共享内存进程间通信呢?

参考:

[1]Windows上C++使用共享内存进行进程间通讯 https://blog.csdn.net/tojohnonly/article/details/70246965

参考:

[1]windows C/C++ 在一个程序中打开,关闭和监视其它的exe程序

https://blog.csdn.net/lumanman_/article/details/72921194

用TestAppStartTime.exe启动app1.exe然后输出测量的时间:

TestAppStartTime.cpp :

// TestAppStartTime.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<Windows.h>
using namespace std;

#define BUF_SIZE 1024


int _tmain(int argc, _TCHAR* argv[])
{
	
	//cout << "startTime=" << startTime << endl;

	// 创建共享文件句柄 
	HANDLE hMapFile = CreateFileMapping(
		INVALID_HANDLE_VALUE,   // 物理文件句柄
		NULL,   // 默认安全级别
		PAGE_READWRITE,   // 可读可写
		0,   // 高位文件大小
		BUF_SIZE,   // 地位文件大小
		L"ShareMemory"   // 共享内存名称
		);
	// 映射缓存区视图 , 得到指向共享内存的指针
	LPVOID lpBase = MapViewOfFile(
		hMapFile,            // 共享内存的句柄
		FILE_MAP_ALL_ACCESS, // 可读写许可
		0,
		0,
		BUF_SIZE
		);
	// 将数据拷贝到共享内存

	long startTime = GetTickCount();

	string startTimestr = std::to_string(startTime);

	strcpy((char*)lpBase, startTimestr.c_str());

	//memcpy((long*)lpBase, &startTime, sizeof(startTime));
	system("app1.exe");

	// 线程挂起等其他线程读取数据
	Sleep(100);

	// 解除文件映射
	UnmapViewOfFile(lpBase);
	// 关闭内存映射文件对象句柄
	CloseHandle(hMapFile);

	system("pause");
	return 0;
}

app1.cpp:

// app1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<Windows.h>
using namespace std;
#define BUF_SIZE 1024

int _tmain(int argc, _TCHAR* argv[])
{
	long t_end = GetTickCount();
	cout << "t_end=" << t_end<< endl;

	char szBuffer[BUF_SIZE] = { 0 };
	long t_start;
	// 打开共享的文件对象
	HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, NULL, L"ShareMemory");
	if (hMapFile)
	{
		LPVOID lpBase = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
		// 将共享内存数据拷贝出来
		//char szBuffer[BUF_SIZE] = { 0 };
		//strcpy(szBuffer, (char*)lpBase);
		//printf("%s", szBuffer);

		//memcpy(&t_start, (long*)lpBase, sizeof(t_start));

		strcpy(szBuffer, (char*)lpBase);

		t_start = atol(szBuffer);
		cout << "t_start=" << t_start << endl;
		cout << "cost time =" << t_end - t_start <<"ms"<< endl;

		// 解除文件映射
		UnmapViewOfFile(lpBase);
		// 关闭内存映射文件对象句柄
		CloseHandle(hMapFile);
	}
	else
	{
		// 打开共享内存句柄失败
		printf("OpenMapping Error");
	}

	system("pause");
	return 0;
}

结果示例:

t_end=117403234
t_start=117403203
cost time =31ms

Cpp 测量程序运行时间 windows进程间通信

猜你喜欢

转载自blog.csdn.net/m0_37357063/article/details/81610272