Parsing datetime strings when running Spring Boot is slow compared to plain Java. Please look at my example code below. Is this performance degradation using Spring a bug, or is it normal that Spring have this much overhead? The Spring perfomance is better with Temurin 21 compared to Temurin 17, but still slow compared to plain java.

Prerequisite: Using a Spring Boot 3.2.3 demo created with Spring Initializr. Running examples with IntelliJ IDEA 2023.3.6

Code example used with Spring Boot and with plain Java project:

        long start = System.currentTimeMillis();
        for (int i = 0; i < 3000000; i++) {
            String dateTime = "2024-03-22T12:34:56.123+00:00";
            ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, ISO_OFFSET_DATE_TIME);
        }
        long end = System.currentTimeMillis();
        System.out.println("Total parse time ms: " + (end - start));

Measurements: Spring Boot 3.2.3 - Java Temurin 17 Total parse time ms: 9330 Spring Boot 3.2.3 - Java Temurin 21: Total parse time ms: 4225

Plain Java project - Java Temurin 17 Total parse time ms: 1547 Plain Java project - Java Temurin 21: Total parse time ms: 1648

Comment From: mhalbritter

There's no Spring Boot code involved in this, so I'm not sure what we can do about that?

Also, your approach to benchmarking is flawed. You should use something like JMH.

Comment From: larsaane

This is the Spring code involved:

@Component
public class DatePerformanceTest {
    public DatePerformanceTest() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 3000000; i++) {
            String dateTime = "2024-03-22T12:34:56.123+00:00";
            ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, ISO_OFFSET_DATE_TIME);
        }
        long end = System.currentTimeMillis();
        System.out.println("Total parse time ms: " + (end - start));

    }
}

Comment From: larsaane

Measuring the total time it takes to do the parsing should be just fine, We don't need JMH to see that it takes longer time running the parsing code in Spring compared to plain java.

Comment From: wilkinsona

This isn't an apples-to-apples comparison and, like @mhalbritter, I don't think it has anything to do with Spring.

When you run the code directly in plain Java, the JVM is in a different state to when the code's run as part of a Spring application. For example, the amount of heap that's available will be different and this will have a knock-on effect on garbage collection. The code that you're measuring creates a significant amount of gargage that has to be collected. When there's less heap available, this collection will have to be performed more often which will slow things down.

You could see this for yourself by running with -verbose:gc and comparing how hard the garbage collector has to work in each case.