Skip to content

Use Stopwatch.GetTimestamp on MacOS #540

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

Merged
merged 14 commits into from
May 10, 2024
Merged

Conversation

bitfaster
Copy link
Owner

@bitfaster bitfaster commented Dec 10, 2023

Stopwatch.GetTimestamp is slightly faster than Environment.TickCount64 on MacOS, therefore use the faster implementation to get a small optimization.

This is the time benchmark result from Github actions running on MacOS:

BitFaster Caching Benchmarks TimeBenchmarks ( NET 6 0)

This is the GetOrAdd bench (baseline v2.5.0):

BenchmarkDotNet v0.13.12, macOS Sonoma 14.4.1 (23E224) [Darwin 23.4.0]
Apple M1 (Virtual), 1 CPU, 3 logical and 3 physical cores
.NET SDK 8.0.204
  [Host]   : .NET 6.0.29 (6.0.2924.17105), Arm64 RyuJIT AdvSIMD
  .NET 6.0 : .NET 6.0.29 (6.0.2924.17105), Arm64 RyuJIT AdvSIMD

BitFaster Caching Benchmarks LruJustGetOrAdd ( NET 6 0)

@bitfaster bitfaster changed the title Use Stopwatch on MacOS Use Stopwatch.GetTimestamp on MacOS May 10, 2024
@coveralls
Copy link

coveralls commented May 10, 2024

Coverage Status

coverage: 99.318% (+0.06%) from 99.263%
when pulling 6370b8d on users/alexpeck/macostime
into aadf0c7 on main.

@bitfaster
Copy link
Owner Author

bitfaster commented May 10, 2024

@bitfaster
Copy link
Owner Author

There is a repeatable very slight gain, but it is close to noise.

BenchmarkDotNet v0.13.12, macOS Sonoma 14.1.1 (23B81) [Darwin 23.1.0]
Apple M2, 1 CPU, 8 logical and 8 physical cores
.NET SDK 8.0.100
  [Host]   : .NET 6.0.29 (6.0.2924.17105), Arm64 RyuJIT AdvSIMD
  .NET 6.0 : .NET 6.0.29 (6.0.2924.17105), Arm64 RyuJIT AdvSIMD
Job=.NET 6.0  Runtime=.NET 6.0  
Method Mean Error StdDev Ratio
DateTimeUtcNow 15.135 ns 0.0100 ns 0.0078 ns 1.00
DateTimeOffsetUtcNow 20.768 ns 0.0170 ns 0.0142 ns 1.37
EnvironmentTickCount 10.624 ns 0.0036 ns 0.0028 ns 0.70
EnvironmentTickCount64 13.357 ns 0.0389 ns 0.0345 ns 0.88
PInvokeTickCount64 NA NA NA ?
StopWatchGetElapsed 12.174 ns 0.0024 ns 0.0019 ns 0.80
StopWatchGetTimestamp 11.585 ns 0.0018 ns 0.0014 ns 0.77
DurationSinceEpoch 9.115 ns 0.0089 ns 0.0074 ns 0.60

LRU bench (with fix)

Method Mean Error StdDev Ratio Allocated
ConcurrentDictionary 3.914 ns 0.0508 ns 0.0475 ns 1.00 -
FastConcurrentLru 5.063 ns 0.0773 ns 0.0723 ns 1.29 -
ConcurrentLru 7.566 ns 0.1483 ns 0.1387 ns 1.93 -
AtomicFastLru 11.718 ns 0.0071 ns 0.0055 ns 3.01 -
FastConcurrentTLru 16.019 ns 0.0046 ns 0.0036 ns 4.11 -
FastConcLruAfterAccess 18.739 ns 0.0030 ns 0.0024 ns 4.81 -
FastConcLruAfter 20.636 ns 0.0063 ns 0.0049 ns 5.30 -
ConcurrentTLru 20.652 ns 0.0108 ns 0.0091 ns 5.30 -
ConcurrentLfu 16.370 ns 0.3445 ns 0.6300 ns 4.35 -
ClassicLru 20.232 ns 0.2783 ns 0.2603 ns 5.17 -
RuntimeMemoryCacheGet 58.372 ns 0.0678 ns 0.0530 ns 14.99 32 B
ExtensionsMemoryCacheGet 25.906 ns 0.0379 ns 0.0296 ns 6.65 24 B

v2.5.0 baseline

Method Mean Error StdDev Ratio Allocated
ConcurrentDictionary 3.935 ns 0.0771 ns 0.0721 ns 1.00 -
FastConcurrentLru 5.013 ns 0.0246 ns 0.0206 ns 1.27 -
ConcurrentLru 7.608 ns 0.0991 ns 0.0927 ns 1.93 -
AtomicFastLru 11.790 ns 0.1040 ns 0.0973 ns 3.00 -
FastConcurrentTLru 16.610 ns 0.0064 ns 0.0054 ns 4.22 -
FastConcLruAfterAccess 18.822 ns 0.0069 ns 0.0058 ns 4.79 -
FastConcLruAfter 21.149 ns 0.0166 ns 0.0139 ns 5.38 -
ConcurrentTLru 21.449 ns 0.0067 ns 0.0052 ns 5.46 -
ConcurrentLfu 15.857 ns 0.0969 ns 0.0906 ns 4.03 -
ClassicLru 21.114 ns 0.0273 ns 0.0213 ns 5.37 -
RuntimeMemoryCacheGet 50.518 ns 0.3877 ns 0.3626 ns 12.84 32 B
ExtensionsMemoryCacheGet 25.940 ns 0.3420 ns 0.3199 ns 6.59 24 B

@bitfaster bitfaster marked this pull request as ready for review May 10, 2024 05:59
@bitfaster bitfaster merged commit 4c91b83 into main May 10, 2024
13 checks passed
@bitfaster bitfaster deleted the users/alexpeck/macostime branch May 10, 2024 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants