简体   繁体   English

计算makefile的构建时间

[英]Calculate makefile build time

Any creative idea how to use enviroment variables to calculate full build time of a makefile? 任何有创意的想法如何使用环境变量来计算Makefile的完整构建时间? Can I store the time somewhere and compare it in the end? 我可以将时间存储在某个地方并最终进行比较吗?

I know the OP meant Windows OS, but for the record, on any Linux machine you can use time : 我知道OP表示Windows操作系统,但据记录,在任何Linux机器上,您都可以使用time

>time make -j
real     0m9.774s
user     0m31.562s
sys      0m1.092s

On Windows 7 you've got PowerShell. 在Windows 7上,您具有PowerShell。 So we'll assume: 因此,我们假设:

  • Your makefile Makefile is in directory C:\\Dev\\ProjDir 您的makefile Makefile位于目录C:\\Dev\\ProjDir
  • Your make tool is mingw32-make and is in your PATH 您的make工具是mingw32-make ,位于PATH

If PowerShell isn't your usual console then do: 如果PowerShell不是您常用的控制台,请执行以下操作:

C:\Dev\ProjDir>powershell

At the PowerShell prompt, run: 在PowerShell提示符下,运行:

PS C:\Dev\ProjDir> Measure-Command { mingw32-make | Out-Default }

This will run your build and display the output, followed by a timing report, eg 这将运行您的构建并显示输出,然后是时序报告,例如

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 244
Ticks             : 2443841
TotalDays         : 2.82851967592593E-06
TotalHours        : 6.78844722222222E-05
TotalMinutes      : 0.00407306833333333
TotalSeconds      : 0.2443841
TotalMilliseconds : 244.3841

Continued for OP's follow-up: 继续进行OP的后续行动:

I was wondering if it is possible to be implemented as part of the makefile, so I can decide which part to measure 我想知道是否可以将其作为makefile的一部分来实现,因此我可以决定要测量的部分

The idea of timing "part of a makefile" does not make clear sense. 计时“ makefile的一部分”的想法没有明确的道理。 When you run make , it parses the entire makefile before it makes anything and works out the entire sequence of steps it must take to make the specified or default target(s). 运行make ,它将在生成任何内容之前先解析整个makefile 并计算出创建指定目标或默认目标所必须执行的全部步骤。 Then, it takes all those steps. 然后,采取所有这些步骤。

Perhaps you want to measure the time taken to make a particular set of targets? 也许您想衡量制定特定目标所需的时间? You can do that in the way already described. 您可以按照已描述的方式进行操作。 For example, suppose that your makefile can make two libraries libfoo.lib and libbar.lib and you would like to time the build of just libfoo.lib . 例如,假设您的makefile可以创建两个库libfoo.liblibbar.lib而您想定时libfoo.lib的构建。 To make libfoo.lib by itself, the make command you would run is: libfoo.lib制作libfoo.lib ,将运行以下make命令:

mingw32-make libfoo.lib

So to time this command, run: 因此,要计时此命令,请运行:

PS C:\Dev\ProjDir> Measure-Command { mingw32-make libfoo.lib | Out-Default }

Or suppose that your makefile makes app.exe from source files foo.c and bar.c , and you would like to time just the build of the object files foo.obj , bar.obj . 或者,假设你的makefile使得app.exe从源文件foo.cbar.c ,和你想的时间目标文件只是构建foo.objbar.obj The make command you would run to build just those object files is: 您将用来构建那些目标文件的make命令是:

mingw32-make foo.obj bar.obj

So you would time it with: 因此,您可以使用以下时间:

PS C:\Dev\ProjDir> Measure-Command { mingw32-make foo.obj bar.obj | Out-Default }

Perhaps you would like to be able to invoke powershell's Measure-Command inside your makefile to time the building of particular targets? 也许您希望能够在makefile中调用powershell的Measure-Command来计时特定目标的建立?

For this, you need a command that invokes PowerShell to run some other command. 为此,您需要一个调用PowerShell的命令以运行其他命令。 That is: 那是:

powershell -c "some other command"

So in your makefile you can add a target for timing the build of any other targets: 因此,在您的makefile文件中,您可以添加一个目标来计时任何其他目标的构建:

.phony: time
time:
    powershell -c "Measure-Command { $(MAKE) $(targets) | Out-Default }"

You would use the time target like so: 您可以这样使用time目标:

C:\Dev\ProjDir>mingw32-make time targets=app.exe

or: 要么:

C:\Dev\ProjDir>mingw32-make time targets="foo.obj bar.obj"

And of course, in your makefile, the commands to build a particular target can include powershell -c "some other command" wherever you like. 当然,在您的makefile中,用于构建特定目标的命令可以在任何您喜欢的地方包括powershell -c "some other command"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM