Speed of dotnet run vs the speed of dotnet for published apps (plus self-contained .NET Core apps)

Dev Tips

The .NET Core team really prides themselves on performance. However, it’s not immediately obvious (as with all systems) if you just do Hello World as a develop. Just today I was doing a Ruby on Rails app in Development Mode with mruby – but that’s not what you’d go to production with.

Let’s look at a great question I got today on Twitter.

Dotnet Run – Builds and Runs Source Code in Development

That’s a great question. If you install .NET Core 2.0 Preview – this person is on a Mac, but you can use Linux or Windows as well – then do just this:

$ dotnet new console
$ dotnet run

It’ll be about 3-4 seconds. dotnet is the SDK and dotnet run will build and run your source code. Here’s a short bit from the docs:

The dotnet run command provides a convenient option to run your application from the source code with one command. It’s useful for fast iterative development from the command line. The command depends on the dotnet build command to build the code. Any requirements for the build, such as that the project must be restored first, apply to dotnet run as well.

While this is super convenient, it’s not totally obvious that dotnet run isn’t something you’d go to production with (especially Hello World Production, which is quite demanding! šŸ˜‰ ).

Dotnet Publish then Dotnet YOUR.DLL for Production

Instead, do a dotnet publish, note the compiled DLL created, then run “dotnet tst.dll.”

For example:

C:UsersscottDesktoptst> dotnet publish
Microsoft (R) Build Engine version 15.3 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

tst -> C:UsersscottDesktoptstbinDebugnetcoreapp2.0tst.dll
tst -> C:UsersscottDesktoptstbinDebugnetcoreapp2.0publish
C:UsersscottDesktoptst> dotnet run .binDebugnetcoreapp2.0tst.dll
Hello World!

On my machine, dotnet run is 2.7s, but dotnet tst.dll is 0.04s.

Dotnet publish –self-contained

I could then publish a complete self-contained app – I’m using Windows, so I’ll publish for Windows but you could even build on a Windows machine but target a Mac runtime, etc and that will make a publish folder.

C:UsersscottDesktoptst> dotnet publish  --self-contained -r win10-x64
Microsoft (R) Build Engine version 15.3 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

tst -> C:UsersscottDesktoptstbinDebugnetcoreapp2.0win10-x64tst.dll
tst -> C:UsersscottDesktoptstbinDebugnetcoreapp2.0win10-x64publish
C:UsersscottDesktoptst> .binDebugnetcoreapp2.0win10-x64publishtst.exe
Hello World!

Note in this case I have a “Self-Contained” app, so all of .NET Core is in that folder and below. Here I run tst.exe, not dotnet.exe because now I’m an end-user.

The results of a published .NET Core App

I hope this helps clear things up.

Sponsor: Check out JetBrains Rider: a new cross-platform .NET IDE. Edit, refactor, test, build and debug ASP.NET, .NET Framework, .NET Core, or Unity applications. Learn more and get access to early builds!

Source link

Leave a Reply