简体   繁体   中英

How much space taken when using System.Linq?

Linq functions on List<T> are awesome, however, in this particular file I'm working on there are only 1 line that use it. So I wanted to know how much space this library took upon importing for use? (I'm using the First function.)

If it's taking too much space then it would make sense to create a custom for loop to iterate through instead. But my code is cleaner when using Linq.

For memory usage I think using Linq and using for don't really make that much difference so I want to know disk space usage if I would use it in Unity3D on mobile devices. If anyone can provide a way to determine disk size of any System library it would be very useful!

using directive doesn't really import anything into the application. It is merely language construct for using namespaces. What is really important if the assembly is actually included into the build. Look here :

Namespace: System.Linq

Assembly: System.Core (in System.Core.dll)

So what really matters is if this assembly is included. And yes, it is included by default in common assemblies that are installed in the system, so by just using Linq you don't make your file bigger.

Edit: question appeared to be in light of mono and Unity. In theory you can build .net app without using System.Core.dll if you are not using external libraries (and some of .net's). But very likely that other libraries that you have to use depend on it. Now regarding mono, it looks like mono treats System.Core as special assembly and it is not necessary to reference it, it is done automatically apparently:

using System;
using System.Linq;

namespace t1
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine (typeof(Enumerable).Assembly.Location);
        }
    }
}

builds just like that dmcs Program.cs . Also if you create empty solution in Xamarin studio it also works and only System.dll is in references.

The .NET framework is installed on your computer. Its not linked into your .exe binary. Its only referenced, which is quite a difference.

Edit:

In followup to Baldricks question, on mobile devices using Unity there should be taken extra care about additional dependencies. They will get bundled into the "player" and add to download size.

Remove unnecessary code dependencies.

http://docs.unity3d.com/Documentation/Manual/iphone-playerSizeOptimization.html

On the other hand LINQ requires System.Core.dll which is an essential for .NET applications. So your bets are high that its already included and LINQ doesn't add to existing download size.

The using directive will just refer to the existing .Net Framework in the computer. It wont always result into directly linking or pulling the assembly in your build. Referenced assemblies do lead to size growths. But, however, the using directive does not tell the compiler to include referenced assemblies. By referencing them in the project, however, you're telling the csc compiler where to find the assembly to link to when compiling. So the assemblies are not referenced unless I reference them via the project or unless they are actually referenced in the code.

Just tried on version 2020.1 of unity and the build size doesn't change after using a function inside linq.

So there is no impact.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM