Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-11667] DateComponents.date is slow on Linux #3282

Open
t-ae opened this issue Oct 25, 2019 · 0 comments
Open

[SR-11667] DateComponents.date is slow on Linux #3282

t-ae opened this issue Oct 25, 2019 · 0 comments

Comments

@t-ae
Copy link

t-ae commented Oct 25, 2019

Previous ID SR-11667
Radar None
Original Reporter @t-ae
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: b3d9b9b721bcab10408898f706efbe14

Issue Description:

DateComponents.date is slow on Linux.

It's slower than DateFormatter.date which should be less efficient.

import Foundation

let iteration = 10_000

let year = 2019
let month = 11
let day = 10
let hour = 10
let minute = 10
let second = 10
let microsecond = 101010

func testDateComponents() {
    var comps = DateComponents(
        calendar:  Calendar(identifier: .gregorian),
        timeZone: TimeZone(secondsFromGMT: 0)!
    )
    comps.year = year
    comps.month = month
    comps.day = day
    comps.hour = hour
    comps.minute = minute
    comps.second = second
    comps.nanosecond = microsecond*1000
    let start = Date()
    var total: TimeInterval = 0
    for _ in 0..<iteration {
        total += comps.date?.timeIntervalSinceNow ?? 0
    }
    print("DateComponents: \(Date().timeIntervalSince(start))sec, \(total)")
}

func testDateFormatter() {
    let df = DateFormatter()
    df.dateFormat = "yyyy/MM/dd HH:mm:ss.SSSSSSS"
    let start = Date()
    var total: TimeInterval = 0
    for _ in 0..<iteration {
        let date = df.date(from: "\(year)/\(month)/\(day) \(hour):\(minute):\(second).\(microsecond)")
        total += date?.timeIntervalSinceNow ?? 0
    }
    print("DateFormatter: \(Date().timeIntervalSince(start))sec, \(total)")
}

testDateComponents()
testDateFormatter()

On macOS(2.7 GHz Intel Core i7):

$ swift --version
Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin18.7.0
$ swiftc -O main.swift
$ ./main
DateComponents: 0.012930989265441895sec, 14041821731.490162
DateFormatter: 0.2265690565109253sec, 13717820468.565754

On Ubuntu(Intel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz):

$ swift --version
Swift version 5.1.1 (swift-5.1.1-RELEASE)
Target: x86_64-unknown-linux-gnu
$ swiftc -O main.swift 
$ ./main
DateComponents: 0.5204849243164062sec, 14032373021.035366
DateFormatter: 0.28744006156921387sec, 14032368951.859718
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant