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

Optimize PerceptualHash implementation #36

Merged
merged 9 commits into from
Nov 7, 2021

Conversation

jaden-young
Copy link
Contributor

Use System.Numerics to optimize the runtime of PerceptualHash.

For the most part this work was done by @jessetrana at our mutual $DAYJOB, I'm just doing the upstreaming here.

I tried my hand at a similar implementation for AverageHash but didn't see any gains.

Benchmarks

Benchmark code is available at simd-optimization-bench. The test suite is a set of 97 images from Unsplash. The benchmark is measuring the time taken to hash the entire set.

Perceptual Hash

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19043
Intel Core i7-4790K CPU 4.00GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.402
  [Host]     : .NET Core 5.0.11 (CoreCLR 5.0.1121.47308, CoreFX 5.0.1121.47308), X64 RyuJIT
  DefaultJob : .NET Core 5.0.11 (CoreCLR 5.0.1121.47308, CoreFX 5.0.1121.47308), X64 RyuJIT

Method Mean Error StdDev Ratio
PerceptualHash 656.86 ms 1.515 ms 1.343 ms 1.00
PerceptualHashOptimized 37.38 ms 0.411 ms 0.385 ms 0.06

All Algorithms

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19043
Intel Core i7-4790K CPU 4.00GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.402
  [Host]     : .NET Core 5.0.11 (CoreCLR 5.0.1121.47308, CoreFX 5.0.1121.47308), X64 RyuJIT
  DefaultJob : .NET Core 5.0.11 (CoreCLR 5.0.1121.47308, CoreFX 5.0.1121.47308), X64 RyuJIT

Method Mean Error StdDev Ratio RatioSD
DifferenceHash 275.3 μs 1.14 μs 1.01 μs 1.00 0.00
AverageHash 284.2 μs 4.52 μs 4.64 μs 1.03 0.02
PerceptualHashOptimized 36,893.8 μs 142.67 μs 133.45 μs 133.96 0.49
PerceptualHash 656,367.7 μs 1,807.57 μs 1,509.40 μs 2,383.89 11.21

@codecov
Copy link

codecov bot commented Nov 7, 2021

Codecov Report

Merging #36 (d4369c2) into develop (715fa98) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##           develop       #36   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines          155       192   +37     
  Branches        42        12   -30     
=========================================
+ Hits           155       192   +37     
Impacted Files Coverage Δ
src/ImageHash/HashAlgorithms/PerceptualHash.cs 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 715fa98...d4369c2. Read the comment docs.

@coenm
Copy link
Owner

coenm commented Nov 7, 2021

@jaden-young , @jessetrana Thanks for your PR.

@coenm coenm merged commit 7c6109c into coenm:develop Nov 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants