Factorio in Unity/C# – Part2

UNITY, WE HAVE A PROBLEM

I love optimization but you reach a point where you only get a fraction out of it. It’s best to stop there.

This point was reached with 150k items. After that 60fps couldn’t be maintained.
I was pretty much in disbelief. How can something so simple reach it’s limits at such a low number.

It was time to reassess my assumptions. Is this what I’m doing even possible on the most basic level?

I wrote a C# console application to test things out. Something really simple. 1 million vector additions. No, not even real vectors. Just adding 2 float coordinates. These are the results:

cSharpVectorAdd

The first one took longer because of the JIT compilation. The rest is below 10ms. Cool, that means it’s definitely possible.

Then I wrote the same program in Unity.

unityVectorAdd

Wait what? 85ms? Factor 10? Are you kidding me? First I thought the problem occurs from wrong measuring but you can pretty much feel the 10 times difference when initiating a new loop with pressing a new key.

So, all optimizations are pretty much in vain unless I find out what’s the big problem here.
Here you can find the TestProjects:
Unity: http://www.enzenebner.com/vectorAdd/vectorAdd_Unity.rar
C#: http://www.enzenebner.com/vectorAdd/vectorAdd_CSharp.rar

I seriously hope someone has insights what’s going on. Maybe I’m doing something wrong but it’s so reduced, I can’t see it.

Update: In this test the main culprit is the GC. Unity is already infamous to have an old/bad GC. The test just shows how big the difference really is.

Read more: Factorio in Unity/C# – Part 3 Goal reached

Advertisements

4 thoughts on “Factorio in Unity/C# – Part2

  1. According to what I read on a post on /r/gamedev, Unity uses an extremely outdated version of C#, along with that comes a very old (and bad)GC. These and similar things related to simply being outdated could play a role.

    Though honestly, I’m really surprised that its THAT much slower, I mean you are only adding!
    You should try to log the results in a build though, the editor usually introduces a lot of lag.

    Like

    1. Try logging the times to a variable in memory and then at the end of the run print out the final time result and number of items so you can calculate the average ms for each addition. I believe the Unity console logging messages will massively slow down your perceived results in the editor. You should see a massive improvement if you don’t log to the console.

      Like

      1. The logging doesn’t really affect the result, it’s such a small fraction of the overall time. I’ve removed it in the updated version though.

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s