Modify

Opened 3 years ago

Closed 3 years ago

#13376 closed enhancement (fixed)

Use Java 8 Date API (JSR 310)?

Reported by: simon04 Owned by: team
Priority: normal Milestone: 16.12
Component: Core Version:
Keywords: java8 jsr310 performance timestamp Cc:

Description

I did some performance tests for timestamp parsing.

package org.openstreetmap.josm.tools.date;

import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.junit.Test;

public class DateUtilsPerformanceTest {

    Stream<String> getTestData() {
        final Random random = new Random(23528390L);
        return IntStream.generate(() -> 0)
                .limit(4_000_000)
                .mapToObj(x -> String.format("%04d-%02d-%02dT%02d:%02d:%02dZ",
                        2000 + random.nextInt(20),
                        1 + random.nextInt(12),
                        1 + random.nextInt(28),
                        random.nextInt(24),
                        random.nextInt(60),
                        random.nextInt(60)
                ));
    }

    @Test
    public void testDateUtils() throws Exception {
        getTestData().forEach(DateUtils::fromString);
    }

    @Test
    public void testZonedDateTime() throws Exception {
        getTestData().forEach(ZonedDateTime::parse);
    }

    @Test
    public void testInstant() throws Exception {
        getTestData().forEach(Instant::parse);
    }
}

The results:

DateUtils.parse using Calendar.set (as is now)14s 769ms
DateUtils.parse using ZonedDateTime.of (see patch)13s 388ms
ZonedDateTime.parse26s 620ms
Instant.parse22s 222ms

So, the current DateUtil is quite performant :).

Attachments (1)

DateUtil.ZonedDateTime.patch (2.5 KB) - added by simon04 3 years ago.

Download all attachments as: .zip

Change History (12)

Changed 3 years ago by simon04

comment:1 Changed 3 years ago by Don-vip

Milestone: 16.08

Go ahead :)

comment:2 Changed 3 years ago by Don-vip

Milestone: 16.0816.09

comment:3 Changed 3 years ago by simon04

In 11035/josm:

see #13376 - Replace Calendar usages with Java 8 Date API

comment:4 Changed 3 years ago by simon04

In 11036/josm:

see #13376 - Fix unit test

comment:5 Changed 3 years ago by simon04

Left to be done/discussed: replace 257 occurrences of Date by Instant, or leave it as it is :)

comment:6 Changed 3 years ago by Don-vip

Given the number of deprecated methods in Date it's almost if the class is now deprecated itself.

I'd say we can change everything that does not impact plugin API.

comment:7 Changed 3 years ago by simon04

Milestone: 16.0916.10

Compatibility will is a major concern since methods like AbstractPrimitive#getTimestamp, AbstractPrimitive#setTimestamp, Note#getCreatedAt, Note#getClosedAt all take/return Dates. So changing that is definitively nothing for the upcoming release. :)

comment:8 Changed 3 years ago by simon04

Milestone: 16.1016.11

Milestone renamed

comment:9 Changed 3 years ago by simon04

In 11288/josm:

see #13376 - Use TimeUnit instead of combinations of 1000/60/60/24

comment:10 Changed 3 years ago by Don-vip

Milestone: 16.1116.12

Milestone renamed

comment:11 Changed 3 years ago by Don-vip

Resolution: fixed
Status: newclosed

New ticket for remaining point: #14176

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain team.
as The resolution will be set.
The resolution will be deleted.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.