←  Pluginy

AMXX.pl: Support AMX Mod X i SourceMod

»

Optymalizacja kodu

Zablokowany

  • +
  • -
Vertricus - zdjęcie Vertricus 09.10.2009

Chciałbym zoptymalizować ten kod gdyż ogólnie chce zobaczyć przykład takiej optymalizacji.
W czym rzecz? Cały czas powtarza mi się pewny fragment kodu i wiem, że da się to skrócić.


/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>

#define PLUGIN "Interp Menu"
#define VERSION "1.0"
#define AUTHOR "Vertricus"
#define Keysinterpowo (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4) // Keys: 12345
new interp[33]

public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_menucmd(register_menuid("interpowo"), Keysinterpowo, "Pressedinterpowo")
	RegisterHam(Ham_Spawn, "player", "spawn", 1);
	// Add your code here...
}


public client_disconnect(id)
{
interp[id]=0
}
public client_connect(id)
{
interp[id]=0
}
public spawn(id)
{
set_task(5.0, "interpownik", id)
}
public interpownik(id)
{
	if (interp[id] == 0) Showinterpowo(id)
	else if (interp[id] == 1)
	{
		console_cmd (id,"cl_updaterate 101")
		server_cmd("wait")
		console_cmd (id,"ex_interp 0.01")
	}
	else if (interp[id] == 2)
	{
		console_cmd (id,"cl_updaterate 101")
		server_cmd("wait")
		console_cmd (id,"ex_interp 0.02")
	}
	else if (interp[id] == 3)
	{
		console_cmd (id,"cl_updaterate 101")
		server_cmd("wait")
		console_cmd (id,"ex_interp 0.03")
	}
	else if (interp[id] == 4)
	{
		console_cmd (id,"cl_updaterate 101")
		server_cmd("wait")
		console_cmd (id,"ex_interp 0.04")
	}
	else if (interp[id] == 5)
	{
		console_cmd (id,"cl_updaterate 101")
		server_cmd("wait")
		console_cmd (id,"ex_interp 0.05")
	}
}
public Showinterpowo(id) {
	show_menu(id, Keysinterpowo, "Wybierz swoj interp:^n1. 0r.01^nw2. 0r.02^nw3. 0r.03^nw4. 0r.04^nw5. 0r.05^n", -1, "interpowo") // Display menu
}
public Pressedinterpowo(id, key) {

	switch (key) {
		case 0: { // 1
			client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
			interp[id]=1
			console_cmd (id,"cl_updaterate 101")
			server_cmd("wait")
			console_cmd (id,"ex_interp 0.01")
		}
		case 1: { // 2
			client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
			interp[id]=2
			console_cmd (id,"cl_updaterate 101")
			server_cmd("wait")
			console_cmd (id,"ex_interp 0.02")
		}
		case 2: { // 3
			client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
			interp[id]=3
			console_cmd (id,"cl_updaterate 101")
			server_cmd("wait")
			console_cmd (id,"ex_interp 0.03")
		}
		case 3: { // 4
			client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
			interp[id]=4
			console_cmd (id,"cl_updaterate 101")
			server_cmd("wait")
			console_cmd (id,"ex_interp 0.04")
		}
		case 4: { // 5
			client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
			interp[id]=5
			console_cmd (id,"cl_updaterate 101")
			server_cmd("wait")
			console_cmd (id,"ex_interp 0.05")
		}
	}
}
Odpowiedz

  • +
  • -
Miczu - zdjęcie Miczu 09.10.2009

Dobra optymalizacje to jedna z moich rzeczy ktore jednak nie praktykuje za czesto O:)

public interpownik(id) 
{ 
    if (interp[id] == 0) Showinterpowo(id) 
    else if (interp[id] == 1) 
    { 
        console_cmd (id,"cl_updaterate 101") 
        server_cmd("wait") 
        console_cmd (id,"ex_interp 0.01") 
    } 
    else if (interp[id] == 2) 
    { 
        console_cmd (id,"cl_updaterate 101") 
        server_cmd("wait") 
        console_cmd (id,"ex_interp 0.02") 
    } 
    else if (interp[id] == 3) 
    { 
        console_cmd (id,"cl_updaterate 101") 
        server_cmd("wait") 
        console_cmd (id,"ex_interp 0.03") 
    } 
    else if (interp[id] == 4) 
    { 
        console_cmd (id,"cl_updaterate 101") 
        server_cmd("wait") 
        console_cmd (id,"ex_interp 0.04") 
    } 
    else if (interp[id] == 5) 
    { 
        console_cmd (id,"cl_updaterate 101") 
        server_cmd("wait") 
        console_cmd (id,"ex_interp 0.05") 
    } 
}
Mozna zamienic na:

public interpownik(id) 
{ 
    if (interp[id] == 0) Showinterpowo(id) 
    else
    { 
        console_cmd (id,"cl_updaterate 101") 
        server_cmd("wait") 
        console_cmd (id,"ex_interp 0.0%d",interp[id]) 
    }
}

I podobnie:
public Pressedinterpowo(id, key) { 

    switch (key) { 
        case 0: { // 1 
            client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.") 
            interp[id]=1 
            console_cmd (id,"cl_updaterate 101") 
            server_cmd("wait") 
            console_cmd (id,"ex_interp 0.01") 
        } 
        case 1: { // 2 
            client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.") 
            interp[id]=2 
            console_cmd (id,"cl_updaterate 101") 
            server_cmd("wait") 
            console_cmd (id,"ex_interp 0.02") 
        } 
        case 2: { // 3 
            client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.") 
            interp[id]=3 
            console_cmd (id,"cl_updaterate 101") 
            server_cmd("wait") 
            console_cmd (id,"ex_interp 0.03") 
        } 
        case 3: { // 4 
            client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.") 
            interp[id]=4 
            console_cmd (id,"cl_updaterate 101") 
            server_cmd("wait") 
            console_cmd (id,"ex_interp 0.04") 
        } 
        case 4: { // 5 
            client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.") 
            interp[id]=5 
            console_cmd (id,"cl_updaterate 101") 
            server_cmd("wait") 
            console_cmd (id,"ex_interp 0.05") 
        } 
    } 
}

Na:
public Pressedinterpowo(id, key) 
{ 
            client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.") 
            interp[id]=key+1
            console_cmd (id,"cl_updaterate 101") 
            server_cmd("wait") 
            console_cmd (id,"ex_interp 0.0%d",key+1) 
}
Odpowiedz

  • +
  • -
mgr inż. Pavulon - zdjęcie mgr inż. Pavulon 09.10.2009

To:

if (interp[id] == 0) Showinterpowo(id)
else if (interp[id] == 1)
{
console_cmd (id,"cl_updaterate 101")
server_cmd("wait")
console_cmd (id,"ex_interp 0.01")
}
else if (interp[id] == 2)
{
console_cmd (id,"cl_updaterate 101")
server_cmd("wait")
console_cmd (id,"ex_interp 0.02")
}
else if (interp[id] == 3)
{
console_cmd (id,"cl_updaterate 101")
server_cmd("wait")
console_cmd (id,"ex_interp 0.03")
}
else if (interp[id] == 4)
{
console_cmd (id,"cl_updaterate 101")
server_cmd("wait")
console_cmd (id,"ex_interp 0.04")
}
else if (interp[id] == 5)
{
console_cmd (id,"cl_updaterate 101")
server_cmd("wait")
console_cmd (id,"ex_interp 0.05")
}

Możesz zamienić na:
if (!interp[id]) Showinterpowo(id)
    else if(1 <= interp[id] <= 5)
    {
      console_cmd (id,"cl_updaterate 101")
      server_cmd("wait")
      console_cmd (id,"ex_interp %f", 0.01*interp[id])
    }

a to:

switch (key) {
case 0: { // 1
client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
interp[id]=1
console_cmd (id,"cl_updaterate 101")
server_cmd("wait")
console_cmd (id,"ex_interp 0.01")
}
case 1: { // 2
client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
interp[id]=2
console_cmd (id,"cl_updaterate 101")
server_cmd("wait")
console_cmd (id,"ex_interp 0.02")
}
case 2: { // 3
client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
interp[id]=3
console_cmd (id,"cl_updaterate 101")
server_cmd("wait")
console_cmd (id,"ex_interp 0.03")
}
case 3: { // 4
client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
interp[id]=4
console_cmd (id,"cl_updaterate 101")
server_cmd("wait")
console_cmd (id,"ex_interp 0.04")
}
case 4: { // 5
client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
interp[id]=5
console_cmd (id,"cl_updaterate 101")
server_cmd("wait")
console_cmd (id,"ex_interp 0.05")
}
}


na:
if (0 <= key <= 4)
{
  client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
  interp[id]=key+1
  console_cmd (id,"cl_updaterate 101")
  server_cmd("wait")
  console_cmd (id,"ex_interp %f", 0.01+0.01*key)
}
Nie potrzebne jest też:
#include <amxmisc>

Czyli razem:
#include <amxmodx>
#include <hamsandwich>

#define Keysinterpowo (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4) // Keys: 12345
new interp[33]

public plugin_init()
{
	register_plugin("Interp Menu", "1.0", "Vertricus")
	register_menucmd(register_menuid("interpowo"), Keysinterpowo, "Pressedinterpowo")
	RegisterHam(Ham_Spawn, "player", "spawn", 1);
}


public client_disconnect(id)
	interp[id]=0

public client_connect(id)
	interp[id]=0

public spawn(id)
	set_task(5.0, "interpownik", id)

public interpownik(id)
	if (!interp[id]) Showinterpowo(id)
	else if(1 <= interp[id] <= 5)
	{
		console_cmd (id,"cl_updaterate 101")
		server_cmd("wait")
		console_cmd (id,"ex_interp %f", 0.01*interp[id])
	}

public Showinterpowo(id)
	show_menu(id, Keysinterpowo, "Wybierz swoj interp:^n1. 0r.01^nw2. 0r.02^nw3. 0r.03^nw4. 0r.04^nw5. 0r.05^n", -1, "interpowo") // Display menu

public Pressedinterpowo(id, key)
	if (0 <= key <= 4)
	{
		client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
		interp[id]=key+1
		console_cmd (id,"cl_updaterate 101")
		server_cmd("wait")
		console_cmd (id,"ex_interp %f", 0.01+0.01*key)
	}


p.s.
kompiluje się, więc możliwe że i działać będzie ;)

p.p.s.
miczu ja cie kiedyś...
Odpowiedz

  • +
  • -
Miczu - zdjęcie Miczu 09.10.2009

Pavulon ladnie, ale moje niestety optymalniejsze i rownie bezpieczne. Stare show_menu zabezpiecza przed wcisnieciem innych przyciskow niz jest zadane, wiec nie trzeba sprawdzac czy jest w zakresie liczb. Wklejenie liczby jest szybsze niz wklejenie i mnozenie liczby.

Poza tym 2 bledy 0.01*x to jest float a nie %d
interp[id]=key w menu jest przesuniecie i musisz dodawac +1
Odpowiedz

  • +
  • -
Vertricus - zdjęcie Vertricus 09.10.2009

Dziękuję bardzo :)
Odpowiedz

  • +
  • -
mgr inż. Pavulon - zdjęcie mgr inż. Pavulon 09.10.2009

Miczu, tak wiem. Nie potrzebnie tak kombinowałem w niektórych miejscach ;)
Odpowiedz

  • +
  • -
Miczu - zdjęcie Miczu 09.10.2009

Vertricus mimo wszystko powinienes stosowac tak jak Pavulon zabezpieczenia przy optymalizacjach. Mozesz sobie tylko pozwolic na je opuszczanie gdy jestes absolutnie pewny, ze wartosci nie przekrocza planowanych wartosci i pamietaj, ze uzytkownicy sa na tyle zdolni, ze moga podawac zle, a nawet ujemne wartosci cvarow calkiem inaczej niz planowales (ahh ile frajdy bylo ze zmieniania ilosci goli na soccerjamie na mniej niz juz jest strzelonych lub kusza lowcy na diablo na predkosc 0 lub ujemne wartosci dmg :D).

Vertricus optymalizacja to glownie jak w matematyce wyciaganie przed nawias i znajdywanie schematow w kodzie (np. liniowa zmiana interpu wzgledem zmiennej), usuwanie zbednego kodu oraz dobieranie momentu wywolania kodu do autentycznego momentu w ktorym jest to konieczne.
Odpowiedz

  • +
  • -
mgr inż. Pavulon - zdjęcie mgr inż. Pavulon 09.10.2009

Tak samo jak taki sam kod jest porozrzucany a nie jak tu w jednym miejscu to wypadało by korzystać z funkcji.
np.
public ustaw_iterp(id, interp)
{
            client_print(id, print_chat, "[EXTREME-SERWER.PL] Interp ustawiony.")
            interp[id]=interp
            console_cmd (id,"cl_updaterate 101")
            server_cmd("wait")
            console_cmd (id,"ex_interp 0.0%d",interp)
}

I wtedy poprzednia funkcja wyglądała by tak:
public Pressedinterpowo(id, key)
{
            ustaw_iterp(id, key+1)
}
Odpowiedz

  • +
  • -
Vertricus - zdjęcie Vertricus 10.10.2009

kusza lowcy na diablo na predkosc 0

Mi się tam bardziej ujemne wartości podobały jak strzelałeś i leciało do tyłu, albo bowdealy na minimalną wartość i spamować bęłtami w spowolnionym tempie ;)

A tak, żeby od tematu nie odbiegać a właściwie to odbiegnę,
chciałbym zacząć php lub c++ czy ktoś mógłby mi na pw wysłać w miarę przystępnie napisany podręcznik ? :)
Odpowiedz

  • +
  • -
darkman - zdjęcie darkman 10.10.2009

kolego, uwierz mi, że nie ma nic lepszego jak Symfonia C++ pana J. Grębosza. utułaj sobie te 100zł i kup w twardej oprawie. będzie na wieki, a wiedze sobie uzupełnisz bardzo dużo.
Odpowiedz

  • +
  • -
Miczu - zdjęcie Miczu 10.10.2009

Mam 3 tomy symfoni od brata, ale nie wczytywalem sie... darkman dzięki za opinie, moze teraz (czyt. kiedys) to poczytam.

Mysle, mysle jak by z tego nie robic offtopu i mam!

Optymalizacja pamieci!
new interp[33]
Mozna zastapic na np.
new max_players =  get_maxplayers ()
new interp[max_players+1]

Jesli [33] to 68 bajtow (licze zero i 33 ktory zawiera znak konca tablicy(?))
Jesli new zmienna to 2 bajty, to jesli np.
Serwer ma max 22 graczy, to z 68 bajtow zajetych spadamy do 46 bajtow. Jesli jest 32 graczy to rosnie do 70 bajtow wiec nie az tak strasznie.
Odpowiedz

  • +
  • -
Vertricus - zdjęcie Vertricus 10.10.2009

Miczu, a czy te bajty to jakaś duża różnica? Czy po prostu to tylko kosmetyka?:)
Odpowiedz

  • +
  • -
Miczu - zdjęcie Miczu 10.10.2009

Zalezy jak duzy plug, w tym twoim to kosmetyka.
Odpowiedz

  • +
  • -
Vertricus - zdjęcie Vertricus 10.10.2009

Jeszcze jedno pytanie czy taka optymalizacja co w twoim 1 poście w tym temacie jest to odciąża coś sam serwer czy tylko zmniejsza objętość pluginu?
Odpowiedz

  • +
  • -
Miczu - zdjęcie Miczu 10.10.2009

Mniej kodu, to mniej ramu zjadanego. Pozbycie sie if'ow, else switch zmniejsza obciazenie (i tak nie zauwazysz) gdyz nie musi ciagle tego sprawdzac, tylko uzyc danych z pamieci.
Odpowiedz
Zablokowany