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

Investigate perf difference in code adding float arrays #23277

Open
adityamandaleeka opened this Issue Mar 15, 2019 · 4 comments

Comments

Projects
None yet
3 participants
@adityamandaleeka
Copy link
Member

adityamandaleeka commented Mar 15, 2019

This function was shared by a user who reported some math operations being slower on Core when compared to Framework. I measured a 6-8% difference on .NET Core 2.2 when compared to Framework 4.7.2.

    [ClrJob, CoreJob]
    public class PerfTests
    {
        const int N = 100_000_000;
        float[] A;
        float[] B;
        float[] R;

        private float Add(float a, float b) => a + b;

        [GlobalSetup]
        public void Setup()
        {
            A = new float[N];
            B = new float[N];
            R = new float[N];

            for (int i = 0; i < N; i++)
            {
                A[i] = i;
                B[i] = i;
                R[i] = i + i;
            }

            var par1 = Expression.Parameter(typeof(float));
            var par2 = Expression.Parameter(typeof(float));
            var add = Expression.Add(par1, par2);
        }

        [Benchmark(OperationsPerInvoke = 100)]
        public unsafe void AddWithFunction()
        {
            int N = A.Length;
            float[] C = new float[N];

            fixed (float* Apinned = A)
            fixed (float* Bpinned = B)
            fixed (float* Cpinned = C)
            {
                float* aptr = Apinned;
                float* bptr = Bpinned;
                float* cptr = Cpinned;

                for (int i = 0; i < N; i++, aptr++, bptr++, cptr++)
                    *cptr = Add(*aptr, *bptr);
            }
        }
    }

I looked at the codegen for AddWithFunction and it is identical on Core and Desktop, so the issue is somewhere else. We'll need to take a closer look and figure out what's different here.

@adityamandaleeka

This comment has been minimized.

Copy link
Member Author

adityamandaleeka commented Mar 15, 2019

@AndyAyersMS

This comment has been minimized.

Copy link
Member

AndyAyersMS commented Mar 15, 2019

Was this on x64? I get maybe 2% slower....

BenchmarkDotNet=v0.11.4, OS=Windows 10.0.18849
Intel Core i7-4770HQ CPU 2.20GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.0.100-preview4-010539
  [Host]     : .NET Core 3.0.0-preview4-27427-5 (CoreCLR 4.6.27427.71, CoreFX 4.7.19.12613), 64bit RyuJIT
  Job-IPVHIQ : .NET Core 2.2.2 (CoreCLR 4.6.27317.07, CoreFX 4.6.27318.02), 64bit RyuJIT
  Job-XVRVJG : .NET Core 3.0.0-preview4-27427-5 (CoreCLR 4.6.27427.71, CoreFX 4.7.19.12613), 64bit RyuJIT
  Job-OHNZAB : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.8.3752.0


|          Method |     Toolchain |     Mean |     Error |    StdDev | Ratio |
|---------------- |-------------- |---------:|----------:|----------:|------:|
| AddWithFunction | .NET Core 2.2 | 2.401 ms | 0.0261 ms | 0.0244 ms |  1.02 |
| AddWithFunction | .NET Core 3.0 | 2.278 ms | 0.0156 ms | 0.0138 ms |  0.97 |
| AddWithFunction |         net47 | 2.348 ms | 0.0162 ms | 0.0151 ms |  1.00 |
@adityamandaleeka

This comment has been minimized.

Copy link
Member Author

adityamandaleeka commented Mar 16, 2019

@AndyAyersMS That's interesting. Yes, I measured on x64 too. I'm on different hardware for what it's worth (Skylake i7-6700).

@AndyAyersMS

This comment has been minimized.

Copy link
Member

AndyAyersMS commented Mar 19, 2019

Marking this as future; we can revisit once the issue is better understood.

@AndyAyersMS AndyAyersMS added this to the Future milestone Mar 19, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.