在Visual Studio中显示生成时间

我们的构建服务器花费太长时间来构建我们的一个C++项目。它使用Visual Studio 2008,运行devenv.com MyApp.sln /Build--见DEVENV命令行开关(尽管这是VS的新版本)。有没有一种方法可以让devenv.com记录在解决方案中构建每个项目所花费的时间,以便我知道应该将精力集中在哪里?

在这种情况下,改进硬件不是一种选择。

我已尝试设置输出详细程度(在菜单工具选项项目和解决方案构建并运行MSBuild项目生成输出详细程度下)。这在IDE中似乎没有任何效果。

当从命令行运行MSBuild时(对于Visual Studio 2008,它需要是MSBuild v3.5),它会在结束时显示经过的总时间,但不会在IDE中显示。

我真的很想为解决方案中的每个项目提供一份耗时报告,这样我就可以找出构建过程所花费的时间。

90208 次浏览

Go to menu ToolsOptionsProjects and SolutionsBuild and RunMSBuild project build output verbosity. Set to "Normal" or "Detailed", and the build time will appear in the output window.

Since your question involves using DevEnv from the command line, I would also suggest using MSBuild (which can build .sln files without modification).

msbuild /fl /flp:Verbosity=diagnostic Your.sln

msbuild /? will show you other useful options for the filelogger.

Menu ToolsOptionsProjects and SolutionsVC++ Project SettingsBuild Timing should work.

If you're stuck on VS2005 you could use the vs-build-timer plugin. At the completion of a build it shows the total time taken and a (optional) summary of each of the project durations.

Disclaimer; I wrote it. And yes, I need to create an installer...one day!

Menu ToolsOptionsProjects and SolutionsBuild and Run

Set "MSBuild project build output verbosity" from "Minimal" to "Normal".

For Visual Studio 2012 you could use the Build Monitor extension.

Visual Studio 2012 - 2019

  • For MSBuild Projects (e.g., all .NET projects): Click ToolsOptions and then select Projects and SolutionsBuild and Run. Change MSBuild project build output verbosity to Normal. So it will display Time Elapsed in every Solution Project it builds. But there is unfortunately no Elapsed Time Sum over all projects. You will also see the Build started Timestamp

  • For C/C++ Projects:

Click ToolsOptions and then select Projects and SolutionsVC++ Project Settings.

Change Build Timing to Yes.

I ended up here because I just wanted the date and time included in the build output. Should others be searching for something similar it's as simple as adding echo %date% %time% to the Pre-build and/or Post-build events under project, PropertiesCompileBuild Events.

Do a build first and see which project is appearing first in the build output (Ctrl + Home in the output window). Right click that project → Project PropertiesCompileBuild EventsPre-build. And echo ###########%date% %time%#############.

So every time you see build results (or during build) do Ctrl + Home in the output window. And somewhere in that area the time and date stares at your face!

Oh and you might end up adding these details to many projects as the build order can change :)


I found a better solution! ###

ToolsOptionsProjects & SolutionsBuild and RunMSBuild project build output verbosity = Normal (or above Minimal). This adds the time in the beginning/top of output window. Ctrl + Home in the output window should do.

If we want to see how much time each projects take then Projects & SolutionsVC++ Project SettingsBuild Timing = yes. It is applicable to all projects; "VC++" is misleading.

If you want to visualize your build, you can use Incredibuild. Incredibuild's now available in standalone-mode (not distributed, but for use only on 8 cores on your local machine) for free as part of Visual Studio 2015 Update 1.

Disclaimer: I work for Incredibuild

If you want to invoke an external program that can track your total build times, you can use the following solution for Visual Studio 2010 (and maybe older). The code below uses CTime by Casey Muratori. Of course, you can also use it to simply print the build time.

Open up the macro explorer, and paste the following before End Module:

Dim buildStart As Date


Private Sub RunCtime(ByVal StartRatherThanEnd As Boolean)
Dim Arg As String
Dim psi As New System.Diagnostics.ProcessStartInfo("ctime.exe")
If StartRatherThanEnd Then
psi.Arguments = "-begin"
Else
psi.Arguments = "-end"
End If
psi.Arguments += " c:\my\path\build.ctm"
psi.RedirectStandardOutput = False
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.UseShellExecute = False
psi.CreateNoWindow = True
Dim process As System.Diagnostics.Process
process = System.Diagnostics.Process.Start(psi)
Dim myOutput As System.IO.StreamReader = process.StandardOutput
process.WaitForExit(2000)
If process.HasExited Then
Dim output As String = myOutput.ReadToEnd
WriteToBuildWindow("CTime output: " + output)
End If
End Sub


Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin
WriteToBuildWindow("Build started!")
buildStart = Date.Now
RunCtime(True)
End Sub


Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
Dim buildTime = Date.Now - buildStart
WriteToBuildWindow(String.Format("Total build time: {0} seconds", buildTime.ToString))
RunCtime(False)
End Sub


Private Sub WriteToBuildWindow(ByVal message As String)
Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim ow As OutputWindow = CType(win.Object, OutputWindow)
If (Not message.EndsWith(vbCrLf)) Then
message = message + vbCrLf
End If
ow.OutputWindowPanes.Item("Build").OutputString(message)
End Sub

The answer was taken from here and here.

Options -> Projects and Solutions -> VC++ Project Settings -> Build Timing

enter image description here

I have created an extension to measure the build times and present the order of events in a graph: Visual Studio Build Timer.

Enter image description here

It is available on the Visual Studio market place and works for Visual Studio 2015, Visual Studio 2017 and Visual Studio 2019.

Apart from showing which projects take longer, the chart displays effective dependencies between them, i.e., projects that need to wait for others, which helps figuring out what dependencies need to break to increase the parallelization of your build.