Skip to content

improve performance of RectHelper.Extend#9

Merged
sdcb merged 2 commits intosdcb:masterfrom
AvenSun:rect-extend
Dec 5, 2023
Merged

improve performance of RectHelper.Extend#9
sdcb merged 2 commits intosdcb:masterfrom
AvenSun:rect-extend

Conversation

@AvenSun
Copy link
Copy Markdown
Contributor

@AvenSun AvenSun commented Dec 1, 2023


BenchmarkDotNet v0.13.10, Windows 10 (10.0.19044.3693/21H2/November2021Update)
.NET SDK 8.0.100
  [Host]     : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2
  Job-LZKMVF : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2
  Job-NCDEPJ : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2

IterationCount=10  LaunchCount=1  WarmupCount=1  

Method Runtime Mean Error StdDev Ratio Allocated Alloc Ratio
ExtendBySdcb .NET 7.0 772.9 ns 7.73 ns 5.12 ns 1.00 - NA
ExtendByAven .NET 7.0 458.9 ns 5.23 ns 2.73 ns 0.59 - NA
ExtendBySdcb .NET 8.0 227.0 ns 3.26 ns 2.16 ns 1.00 - NA
ExtendByAven .NET 8.0 176.6 ns 0.92 ns 0.55 ns 0.77 - NA

the code of benchmark.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sdcb.OpenVINO.Benchmarks
{
    [SimpleJob(runtimeMoniker: RuntimeMoniker.Net70, launchCount: 1, warmupCount: 1, iterationCount: 10)]
    [SimpleJob(runtimeMoniker: RuntimeMoniker.Net80, launchCount: 1, warmupCount: 1, iterationCount: 10)]
    [MemoryDiagnoser]
    [HtmlExporter]
    [MarkdownExporterAttribute.GitHub]
    public class RectExtend
    {
        private Random random;
        private List<Rect> rects;

        [GlobalSetup]
        public void GlobalSetup()
        {
            this.random = new();
            int rectNum = 100;
            this.rects = new(rectNum);
            for (int i = 0; i < rectNum; i++)
            {
                this.rects.Add(new Rect(
                    this.random.Next(0, 640), this.random.Next(0, 640),
                    this.random.Next(0, 640), this.random.Next(0, 640))
                    );
            }
        }

        [Benchmark(Description = "ExtendBySdcb", Baseline = true)]
        public void ExtendBySdcb()
        {
            for (int i = 0; i < this.rects.Count; i += 2)
            {
                ExtendSdcb(this.rects[i], this.random.Next(0, 88));
            }
        }

        [Benchmark(Description = "ExtendByAven")]
        public void ExtendByAven()
        {
            for (int i = 0; i < this.rects.Count; i += 2)
            {
                ExtendAven(this.rects[i], this.random.Next(0, 88));
            }
        }

        static Rect ExtendSdcb(in Rect rect, int extendLength)
        {
            return Rect.FromLTRB(rect.Left - extendLength, rect.Top - extendLength, rect.Right + extendLength, rect.Bottom + extendLength);
        }

        static Rect ExtendAven(in Rect rect, int extendLength)
        {
            rect.Inflate(extendLength, extendLength);
            return rect;
        }
    }
}

@sdcb
Copy link
Copy Markdown
Owner

sdcb commented Dec 1, 2023

The problem of this is imcoming Rect should be const, this would impact the imcoming Rect, I think?

@AvenSun
Copy link
Copy Markdown
Contributor Author

AvenSun commented Dec 1, 2023

sorry for that, the keyword in is ignored.
fixed now. new benchmark result as fellows.


BenchmarkDotNet v0.13.10, Windows 10 (10.0.19044.3693/21H2/November2021Update)
.NET SDK 8.0.100
  [Host]     : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2
  Job-PXWREW : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2
  Job-LCQJJH : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2

IterationCount=10  LaunchCount=1  WarmupCount=1  

Method Runtime Mean Error StdDev Ratio Allocated Alloc Ratio
ExtendBySdcb .NET 7.0 1,477.7 ns 10.54 ns 6.27 ns 1.00 - NA
ExtendByAven .NET 7.0 1,031.4 ns 6.97 ns 4.61 ns 0.70 - NA
ExtendBySdcb .NET 8.0 441.5 ns 1.91 ns 1.26 ns 1.00 - NA
ExtendByAven .NET 8.0 325.4 ns 0.99 ns 0.52 ns 0.74 - NA

@sdcb
Copy link
Copy Markdown
Owner

sdcb commented Dec 2, 2023

I have a better suggestion, can you try delete the in keyword and test again, I think we can not using the in keyword in this case, it will copy automatically.

@sdcb
Copy link
Copy Markdown
Owner

sdcb commented Dec 4, 2023

@AvenSun can you run the perf test again without the in keyword? thanks

@AvenSun
Copy link
Copy Markdown
Contributor Author

AvenSun commented Dec 5, 2023

hi @sdcb
sorry for the delay.
benchmark without in keyword as follows


BenchmarkDotNet v0.13.10, Windows 10 (10.0.19044.3693/21H2/November2021Update)
.NET SDK 8.0.100
  [Host]     : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2
  Job-ERUGJG : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2
  Job-HGGJRE : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2

IterationCount=10  LaunchCount=1  WarmupCount=1  

Method Runtime Mean Error StdDev Ratio RatioSD Allocated Alloc Ratio
ExtendBySdcb .NET 7.0 1,939.1 ns 480.54 ns 317.85 ns 1.00 0.00 - NA
ExtendByAven .NET 7.0 995.2 ns 39.29 ns 25.99 ns 0.53 0.09 - NA
ExtendBySdcb .NET 8.0 440.6 ns 0.77 ns 0.46 ns 1.00 0.00 - NA
ExtendByAven .NET 8.0 326.1 ns 1.07 ns 0.64 ns 0.74 0.00 - NA

@sdcb sdcb merged commit 7ba2655 into sdcb:master Dec 5, 2023
@sdcb
Copy link
Copy Markdown
Owner

sdcb commented Dec 5, 2023

Thanks

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