Startup, measured: C# and Java

The time from a command line to the first line of output is the first thing a user of a command-line tool notices, and it is mostly the runtime and not the program. The task is one line of output, built once and started five times in each language, with the shell timing the process.

The task

The C# program is one statement; the Java one is one class with a main:

Console.WriteLine("hello 1.0");

public class Hello { public static void main(String[] args) { System.out.println("hello 1.0"); } }

Both were built first, and then started from the built artifact: the C# dll with dotnet, the Java class with java -cp out. A dotnet run would have measured the build as well, which is why the command on the page names the dll:

dotnet bin/Release/net10.0/Program.dll
echo "status: $?"

hello 1.0
status: 0

java -cp out Hello
echo "status: $?"

hello 1.0
status: 0

The numbers

ProgramRuns (seconds)MinMedian
C#, dotnet Program.dll0.099, 0.071, 0.071, 0.071, 0.0720.0710.071
Java, java -cp out Hello0.052, 0.053, 0.054, 0.053, 0.0520.0520.053

Java started about 20 milliseconds earlier here, and the first C# run was 28 milliseconds slower than the next four. Both numbers include the process start, the runtime start and the printing of one line; a machine with a warm file cache and no other work gave the same figures on a second series.

What differs

The last page puts the measurements and the language features together: when C# is the right choice, and when it is not.

Exercises

  1. Measure dotnet run for the same program and compare it with the dll.
  2. Measure a hello program in a third language of the image, if it has one, and add a row.
  3. Publish the C# program as a single file and measure the startup again.
  4. Explain what a warm file cache does to both numbers.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What was the median startup of the C# program?

    Answer

    0.071 s

    The measured table reads 0.071 for the median.

  2. Why start the dll and not dotnet run?

    Answer

    dotnet run includes the build, which is not startup

    The page measures the program, not the build.

  3. What does this program print? Console.WriteLine("hello 1.0");

    Answer

    hello 1.0

    That is the hello program of the page.

  4. What is the price both runtimes pay for a program that does nothing?

    Answer

    50 to 100 milliseconds of runtime start

    Both medians are in that range.

  5. What does this program print? Console.WriteLine(1);

    Answer

    1

    One line of output is printed.

  6. Which C# option trades size for startup?

    Answer

    ReadyToRun

    Lesson 108 measured a ReadyToRun build of five files.

  7. What does jlink give a Java program?

    Answer

    A smaller runtime image with only the modules the program needs

    It is the Java answer to a smaller, faster-starting deployment.

  8. What does this program print? Console.WriteLine(0.053 < 0.071);

    Answer

    True

    The Java median was 0.053 and the C# 0.071.

  9. When does a 70 millisecond start matter?

    Answer

    When a script starts the tool many times

    A thousand invocations turn 70 ms into 70 seconds.

  10. What did the second run series of the page show?

    Answer

    The same figures on a warm cache

    The page says the figures were the same on a second series.

The C# program was built with the .NET SDK 10.0.303 and the Java program with OpenJDK 21.0.13.1 (javac 21.0.13.1), both run on Alpine Linux 3.24.3 (x86_64, four processors) in a Docker container.

Article author: Arthur Isaev