question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why is EF Core 3.1.1 slower than EF Core 2.2.6?

See original GitHub issue

Save 1 record EF Core 2.2.6 Run 1: 96.6822ms. Run 2: 2.1267ms. Run 3: 1.6975ms. EF Core 3.1.1 Run 1: 267.0279ms. Run 2: 7.1322ms. Run 3: 4.0974ms.

Retrieve 1 record EF Core 2.2.6 Run 1: 0.0218ms. Run 2: 0.024ms. Run 3: 0.0123ms. EF Core 3.1.1 Run 1: 0.0729ms. Run 2: 0.0204ms. Run 3: 0.0167ms.

Save 10,000 records EF Core 2.2.6 Run 1: 202.2927ms. Run 2: 58.1302ms. Run 3: 50.2324ms. EF Core 3.1.1 Run 1: 825.3337ms. Run 2: 207.5495ms. Run 3: 216.0945ms.

Retrieve 10,001 records EF Core 2.2.6 Run 1: 0.0266ms. Run 2: 0.0302ms. Run 3: 0.0503ms. EF Core 3.1.1 Run 1: 0.0273ms. Run 2: 0.0302ms. Run 3: 0.0754ms.

Stopwatch sw = new Stopwatch();
            string s1 = "";
            string s2 = "";
            string s3 = "";
            string s4 = "";
            for (int j = 1; j <= 3; j++)
            {
                // Add 1 record
                sw.Reset();
                sw.Start();
                TestHeader o = new TestHeader()
                {
                    Name = $"Tester"
                };

                TestContext db = new TestContext();
                db.Add(o);
                db.SaveChanges();
                sw.Stop();
                TimeSpan ts = sw.Elapsed;

                s1 += $"Run {j}: {ts.TotalMilliseconds.ToString()}ms.  ";
                
                // Retrieve 1 record
                sw.Reset();
                sw.Start();
                db = new TestContext();
                var item = db.TestHeader;
                sw.Stop();
                ts = sw.Elapsed;

                s2 += $"Run {j}: {ts.TotalMilliseconds.ToString()}ms.  ";

                // Add 10,000 records
                sw.Reset();
                sw.Start();
                List<TestHeader> list = new List<TestHeader>();
                for (int i = 0; i <= 10000; i++)
                {
                    TestHeader user = new TestHeader()
                    {
                        Name = $"User {i}"
                    };

                    list.Add(user);
                }

                db = new TestContext();
                db.BulkInsert(list);
                sw.Stop();
                ts = sw.Elapsed;

                s3 += $"Run {j}: {ts.TotalMilliseconds.ToString()}ms.  ";

                // Retrieve 10,001 records
                sw.Restart();
                sw.Start();
                db = new TestContext();
                var s = db.TestHeader;
                sw.Stop();
                ts = sw.Elapsed;
                s4 += $"Run {j}: {ts.TotalMilliseconds.ToString()}ms.  ";

                db.RemoveRange(s);
                db.SaveChanges();
            }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sgashuacommented, Jan 31, 2020

Alright, looks not so much difference. Thanks. How to use milliseconds instead of nanoseconds?

[EDITED. Changed ns to ms. Thanks @roji ] EFCore 2.2.6

             
MethodMean [ms]Error [ms]StdDev [ms]Median [ms]
Save_1_Record1.4114 ms0.0702 ms0.2036 ms1.3827 ms
Load_1_Record0.0009 ms0.0000 ms0.0000 ms0.0009 ms
Save_10000_Record48.8672 ms2.5019 ms7.1381 ms46.7122 ms
Load_10000_Record0.0008 ms0.0000 ms0.0000 ms0.0008 ms

EFCore 3.1.1

             
MethodMean [ms]Error [ms]StdDev [ms]Median [ms]
Save_1_Record1.6398 ms0.1149 ms0.3241 ms1.6005 ms
Load_1_Record0.0009 ms0.0000 ms0.0000 ms0.0009 ms
Save_10000_Record45.4850 ms1.5392 ms4.4899 ms44.1879 ms
Load_10000_Record0.0009 ms0.0000 ms0.0000 ms0.0009 ms
public class TestBenchmark
    {
        public void Load()
        {
            var db = new TestContext();
            var item = db.TestHeader;
        }
        public void SaveBulk()
        {
            List<TestHeader> list = new List<TestHeader>();
            for (int i = 1; i <= 10000; i++)
            {
                TestHeader user = new TestHeader()
                {
                    Name = $"User {i}"
                };

                list.Add(user);
            }

            var db = new TestContext();
            db.BulkInsert(list);
        }
        public void SaveSingle()
        {
            var db = new TestContext();
            TestHeader o = new TestHeader()
            {
                Name = $"Tester"
            };
            db.Add(o);
            db.SaveChanges();
        }

        [Benchmark]
        public void Save_1_Record()
        {
            SaveSingle();
        }

        [Benchmark]
        public void Load_1_Record()
        {
            Load();
        }

        [Benchmark]
        public void Save_10000_Record()
        {
            SaveBulk();
        }

        [Benchmark]
        public void Load_10000_Record()
        {
            Load();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var summary = BenchmarkRunner.Run<TestBenchmark>();
        }
    }
1reaction
rojicommented, Jan 31, 2020

@sgtagan your benchmark is problematic in various ways. There is no warm-up phase, meaning that you include various start-up costs in your benchmark (e.g. query compilation). Unless I’m missing something you’re also only running each scenario once, which doesn’t provide much certainty as to the results.

I’d suggest you take a look at BenchmarkDotNet, which is a superb library which takes care of all the common pitfalls of benchmarking. It’s really easy to start with and provides very reliable results. If you have a BDN scenario which performs much worse on 3.1, please post the full benchmark code for that and we’ll look into it.

@AndriySvyryd could you post the benchmark code?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why EF Core 3.1.1 slower than EF Core 2.2.6? Do you ...
I tested by saving 10,000 records and retrieving 10,000 records. All are in new projects. No additional codes. EF Core 2.2.6: Save 10,000 ......
Read more >
Upgrading Entity Framework Core 2.2 | by Łukasz Wiśniewski
It is not obvious why is the EF Core 3.1 significantly slower than 3 queries in this case.
Read more >
Entity Framework Core 3.1 Bug vs 2.2, Speed and Memory ...
EF 3.1.8 is never faster than 80ms and six requests take more than 100ms. Overall Elapsed -. EF 2.2.6 returns most requests in...
Read more >
Compare EF6 and EF Core
Guidance on how to choose between EF6 and EF Core.
Read more >
Your honest opinion on EF Core? : r/dotnet
So while EF Core is much faster than EF 6, it's still going to be slower than data access designed by someone who...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found