Skip to content

refine distance of rectangle#8

Merged
sdcb merged 1 commit intosdcb:masterfrom
AvenSun:box-distance
Dec 1, 2023
Merged

refine distance of rectangle#8
sdcb merged 1 commit intosdcb:masterfrom
AvenSun:box-distance

Conversation

@AvenSun
Copy link
Copy Markdown
Contributor

@AvenSun AvenSun commented Nov 30, 2023

we can get much more performance improvement.


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-KHKRLK : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2
  Job-EYLDJJ : .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
DistanceBySdcb .NET 7.0 534.3 ns 13.60 ns 9.00 ns 1.00 - NA
DistanceByAven .NET 7.0 403.2 ns 2.48 ns 1.64 ns 0.75 - NA
DistanceBySdcb .NET 8.0 554.4 ns 5.64 ns 3.73 ns 1.00 - NA
DistanceByAven .NET 8.0 162.7 ns 2.40 ns 1.43 ns 0.29 - NA
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using OpenCvSharp;
using OpenCvSharp.Text;
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 BoxDistance
    {
        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 = "DistanceBySdcb", Baseline = true)]
        public void DistanceBySdcb() 
        { 
            for (int i = 0;i < this.rects.Count; i += 2)
            {
                DistanceSdcb(this.rects[i], this.rects[i + 1]);
            }
        }

        [Benchmark(Description = "DistanceByAven")]
        public void DistanceByAven()
        {
            for (int i = 0; i < this.rects.Count; i += 2)
            {
                DistanceAven(this.rects[i], this.rects[i + 1]);
            }
        }

        static float DistanceSdcb(in Rect box1, in Rect box2)
        {
            int x1_1 = box1.X;
            int y1_1 = box1.Y;
            int x2_1 = box1.Right;
            int y2_1 = box1.Bottom;

            int x1_2 = box2.X;
            int y1_2 = box2.Y;
            int x2_2 = box2.Right;
            int y2_2 = box2.Bottom;

            float dis = Math.Abs(x1_2 - x1_1) + Math.Abs(y1_2 - y1_1) + Math.Abs(x2_2 - x2_1) + Math.Abs(y2_2 - y2_1);
            float dis_2 = Math.Abs(x1_2 - x1_1) + Math.Abs(y1_2 - y1_1);
            float dis_3 = Math.Abs(x2_2 - x2_1) + Math.Abs(y2_2 - y2_1);
            return dis + Math.Min(dis_2, dis_3);
        }

        static float DistanceAven(in Rect box1, in Rect box2)
        {
            int dx1 = Math.Abs(box2.X - box1.X);
            int dy1 = Math.Abs(box2.Y - box1.Y);
            int dx2 = Math.Abs(box2.Right - box1.Right);
            int dy2 = Math.Abs(box2.Bottom - box1.Bottom);

            float dis = dx1 + dy1 + dx2 + dy2;
            float dis_2 = dx1 + dy1;
            float dis_3 = dx2 + dy2;

            return dis + Math.Min(dis_2, dis_3);
        }

    }
}

@sdcb sdcb merged commit d55e50b into sdcb:master Dec 1, 2023
@sdcb
Copy link
Copy Markdown
Owner

sdcb commented Dec 1, 2023

thanks so much!

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