You are not logged in.

Dear visitor, welcome to sourceserver.info. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Impact

Super Moderator

  • "Impact" started this thread

Posts: 1,276

wcf.user.option.userOption53: Nein

  • Send private message

1

Saturday, March 10th 2012, 7:17pm

Optimierung die Zweite

Optimieren von Scripts ist ein wichtiger Grundsatz den jeder Entwickler befolgen sollte, allerdings ist es nicht immer leicht die "langsamen" Stellen in seinem Code zu finden.
In dem zweiten Post dieser kleinen Reihe möchte ich ein weiteres Beispiel zeigen wie man seinen Code optimieren kann.

Ich hatte mir zum testen dieses kleine Stückchen Code genommen.

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
static Modulo;

for(new i<= RUNSi++)
{
    Modulo 2;
    
    if(Modulo == 0)
    {
        Func_Nothing();
    }
}

RUNS ist ein define den ich auf 1000 Durchläufe festgelegt habe.

Diese Funktion macht folgendes, wenn die Variable i welche jeden Durchlauf um 1 erhöht wird eine gerade Zahl ist (2, 4, 6, 8, ...) dann wird eine Funktion aufgerufen die keinen Inhalt hat.
Im Durchschnitt kam ich auf: 0.000006 Sekunden

Verändern wir das Stückchen Code nun ein kleines bisschen

PHP Source code

1
2
3
4
5
6
7
8
9
for(new i<= RUNSi++)
{
    Modulo 2;
    
    if(!Modulo)
    {
        Func_Nothing();
    }
}

Mit dieser kleinen Änderung kam ich auf durchschnittlich: 0.000008 Sekunden

Ich werde nun noch einige andere Beispiele der selben Funktion und deren Zeiten auflisten, das Ergebnis wird euch sicher erstaunen.

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
for(new i<= RUNSi++)
{
    Modulo 2;
    
    if(Modulo != 0)
    {
        continue;
    }
    
    Func_Nothing();
    
}

0.000007 Sekunden


PHP Source code

1
2
3
4
5
6
7
8
9
10
11
for(new i<= RUNSi++)
{
    Modulo 2;
    
    if(Modulo <= 1)
    {
        continue;
    }
    
    Func_Nothing();
}

0.000004 Sekunden


PHP Source code

1
2
3
4
5
6
7
8
9
10
11
for(new i<= RUNSi++)
{
    Modulo 2;
    
    if(Modulo >= 0)
    {
        continue;
    }
    
    Func_Nothing();
}

0.000004 Sekunden


PHP Source code

1
2
3
4
5
6
7
8
9
for(new i<= RUNSi++)
{
    Modulo 2;
    
    if(Modulo >= 0)
    {
        Func_Nothing();
    }
}

0.000009 Sekunden


PHP Source code

1
2
3
4
5
6
7
8
9
for(new i<= RUNSi++)
{
    Modulo 2;
    
    if(Modulo <= 0)
    {
        Func_Nothing();
    }
}

0.000006 Sekunden


PHP Source code

1
2
3
4
5
6
7
8
9
for(new i<= RUNSi++)
{
    Modulo 2;
    
    if(!(Modulo 0))
    {
        Func_Nothing();
    }
}

0.000010 Sekunden

Es gibt natürlich noch dutzende andere Möglichkeiten diese Abfrage zu schreiben, aber dies waren die am häufigst gesehen.
Allerdings gilt auch hier dieses Prinzip.
Hier noch der gesamte Code zum selbst testen

Spoiler Spoiler


PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <sourcemod>
#include <profiler>
#pragma semicolon 1



new Handle:g_prof;



public OnPluginStart()
{
    RegConsoleCmd("sm_test"Command_Test);
    g_prof CreateProfiler();
}



#define RUNS 1000


public Action:Command_Test(clientargs)
{
    StartProfiling(g_prof);

    static Modulo;

    
    for(new i<= RUNSi++)
    {
        Modulo 2;
        
        if(Modulo == 0)
        {
            Func_Nothing();
        }
        
    }
    
    
    
    StopProfiling(g_prof);
    PrintToServer("Benchmark: %f seconds"GetProfilerTime(g_prof));

    return Plugin_Handled;
}


stock Func_Nothing()
{
}



Interessant, nicht wahr?

MfG
Impact