Optymalizacja kodu
Vertricus
09.10.2009
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") } } }
Miczu
09.10.2009
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) }
mgr inż. Pavulon
09.10.2009
Możesz zamienić na: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")
}
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ś...
Miczu
09.10.2009
Poza tym 2 bledy 0.01*x to jest float a nie %d
interp[id]=key w menu jest przesuniecie i musisz dodawac +1
mgr inż. Pavulon
09.10.2009
Miczu
09.10.2009
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.
mgr inż. Pavulon
09.10.2009
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) }
Vertricus
10.10.2009
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 tempiekusza lowcy na diablo na predkosc 0
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 ?
darkman
10.10.2009
Miczu
10.10.2009
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.