Which has the best performance? static objects

I developed a console application in C # to combine and split huge files (about 4 GB in size) using OOP design. This includes reading / writing xml, flat files and images. I have classes for readers and writers.

The merger took about 00:12, and the splitting took more than 4:30 hours. Then I increased the separation performance by reaching 00:50, distributing the output files into subdirectories, rather than using a single directory.

My boss asks me to convert everything to static procedural programming, but not objects. He says that 00:12 for a merger compared to 00:50 for a merger is not balanced. He wants to split at 00:30 minutes, turning into a static.

Now I know that static calls are faster according to this . However, I do not agree that all static ones will be better, since I will have to use the "ref" and "out" parameters in the methods.

My questions:

  • What is the reason for splitting files into a subdirectory much faster than using a single output directory? (i.e. for a huge number of files> 200,000).
  • Is there a better way than converting my code from object to static in order to achieve better performance?
+3
source share
7 answers

Profiled your program?

You must comment on your code. Objects are fast, non-optimal code is dead slowly.

, , I/O ( , , ).

, , , , . , ?

+13

, , . . , (, ), IO, . , ( ). , , , ..

:

static: 154ms
instance: 156ms

, 2 50 ! ...

:

class Program
{
    static void Main()
    {
        StaticMethod(); // JIT
        Program p = new Program();
        p.InstanceMethod(); // JIT

        const int LOOP = 50000000; // 50M
        Stopwatch watch = Stopwatch.StartNew();
        for (int i = 0; i < LOOP; i++) StaticMethod();
        watch.Stop();
        Console.WriteLine("static: " + watch.ElapsedMilliseconds + "ms");

        watch = Stopwatch.StartNew();
        for (int i = 0; i < LOOP; i++) p.InstanceMethod();
        watch.Stop();
        Console.WriteLine("instance: " + watch.ElapsedMilliseconds + "ms");
    }
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
    void InstanceMethod() { }
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
    static void StaticMethod() { }
}

:

(), 20 (if (i % 20 == 0) p = new Program();), :

static: 174ms
instance: 873ms

- , , 50 , !

+12

, IO, . - OO . ( ) .

, , . , Microsoft, Jetbrain dotTrace profiler. , - , .

IO , , ? ?

, . , , . , 200 000 . , , .

, ?

+6

1: . - Windows ( NTFS, ). .

2, , . , . . , - . , , .

, Ants, , . , , , , . , , , .

+3

  • 20 -30k /. . Ntfs

  • , OO , OO, . , , . . -

+2

, . ?

, , . , .

0
  • It is impossible to answer without knowing your FS. But, as others have pointed out, FSes are usually not optimized for massive collapsed directory trees.
  • I think that rejecting OOP due to a possible (you did not profile) ~ 10% speed increase is ridiculous, especially when the page says: "Please do not take this data too literally."

Finally, although you did not provide a lot of information, I see no reason to think that this “imbalance” is strange. Recording is slower, sometimes significantly.

0
source

Source: https://habr.com/ru/post/1710130/


All Articles