Tips & Tricks
#41

Quote:
Originally Posted by MrDeath537
Посмотреть сообщение
Hey Slice, can you add what does the '~' means?

Thanks
It's fully explained in An in-depth look at binary and binary operators!
Reply
#42

Nice tut
Reply
#43

Wow, I always doubted that the fastest play loop wouldn't work as it would finish before all connected players were cycled through a loop.. guess I was wrong as I finally tested it.

Код:
[12:20:55] Connected Players = 26 | Phantom Slots = 45
[12:20:55] ID: 0 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 1 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 2 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 3 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 4 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 5 | Connected: 0 | Received loop: No
[12:20:55] ID: 6 | Connected: 0 | Received loop: No
[12:20:55] ID: 7 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 8 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 9 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 10 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 11 | Connected: 0 | Received loop: No
[12:20:55] ID: 12 | Connected: 0 | Received loop: No
[12:20:55] ID: 13 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 14 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 15 | Connected: 0 | Received loop: No
[12:20:55] ID: 16 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 17 | Connected: 0 | Received loop: No
[12:20:55] ID: 18 | Connected: 0 | Received loop: No
[12:20:55] ID: 19 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 20 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 21 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 22 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 23 | Connected: 0 | Received loop: No
[12:20:55] ID: 24 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 25 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 26 | Connected: 0 | Received loop: No
[12:20:55] ID: 27 | Connected: 0 | Received loop: No
[12:20:55] ID: 28 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 29 | Connected: 0 | Received loop: No
[12:20:55] ID: 30 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 31 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 32 | Connected: 0 | Received loop: No
[12:20:55] ID: 33 | Connected: 0 | Received loop: No
[12:20:55] ID: 34 | Connected: 0 | Received loop: No
[12:20:55] ID: 35 | Connected: 0 | Received loop: No
[12:20:55] ID: 36 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 37 | Connected: 0 | Received loop: No
[12:20:55] ID: 38 | Connected: 0 | Received loop: No
[12:20:55] ID: 39 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 40 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 41 | Connected: 1 | Received loop: Yes
[12:20:55] ID: 42 | Connected: 0 | Received loop: No
[12:20:55] ID: 43 | Connected: 0 | Received loop: No
[12:20:55] ID: 44 | Connected: 1 | Received loop: Yes
Reply
#44

Can you explain what is "?" ":"
Reply
#45

Quote:
Originally Posted by Ricop522
Посмотреть сообщение
Can you explain what is "?" ":"
Oh my god, he already explained it, it's the ternary operator, a quick / better 'if else if' statement.

pawn Код:
c = ( a == b ) ? d : e;
//   if-^   then-^   ^-else
Reply
#46

srry for bump, but do I can use "char arrays" in enums? if yes, then please, anyone, give me an example
Reply
#47

is there anything like char arrays for floats?
Reply
#48

Quote:
Originally Posted by pagie1111
Посмотреть сообщение
Wow, I always doubted that the fastest play loop wouldn't work as it would finish before all connected players were cycled through a loop.. guess I was wrong as I finally tested it.
It's the fastest plain player loop. Using Y_Less's foreach, for example, is faster.

@BaubaS: Not sure.
@Donya: Not sure.
Reply
#49

I think I should bump this topic.
Just know something about char-array
Reply
#50

Quote:
Originally Posted by letters
Посмотреть сообщение
I think I should bump this topic.
Just know something about char-array
Just don't forget about those curly braces.
Reply
#51

Quote:
Originally Posted by HellSphinX
Посмотреть сообщение
Say the player isn't an admin, now we have declared str and armour for nothing while you could have declared them ONLY when you need them. That's what I'm at.
I specifially said "in one place". That does not necessarily refer to the top of the block!
Reply
#52

itt: blind leading the blind
Reply
#53

After seeing this: https://sampforum.blast.hk/showthread.php?tid=392181

A trick I always use so I never forget brackets anywhere. Immediately write the corresponding bracket before you start coding the statements. Maybe a stupid trick but it saves me a lot of time trying to find missing brackets when code gets rather big.

Bad example:
pawn Код:
stock something() {
    // I'll write some code here first
    // Then I'll do some more stuff
    // Now I need an if statement possible
    if(someVariable == true) {
        // Now I write some code here
See what I'm getting at? When you finish your code you may forget to put an ending bracket (stupid example here since it's only 2 of them--but you get the point).

Good example:
pawn Код:
stock something() {
    // I write NOTHING here yet, I first write the ending bracket
}

// Only NOW I can continue writing code in the function
Obviously, proper IDE's do this for you. Also, tabulate!
Reply
#54

lol. do you know about benchmarking? so do its yourself.
I never says anything without a reason/proof.

Normal condition: 10760068 MS
Ternary condition: 10955372 MS


Код:
#include <a_samp>

main()
{
	new StartTick, StopTick;
	StartTick = GetTickCount();

	for(new i; i < 1000000; i ++)
	{
		StartTick = (StartTick == 1) ? 0 : 1;
		StartTick = (StartTick == 1) ? 0 : 1;
		StartTick = (StartTick == 1) ? 0 : 1;
		StartTick = (StartTick == 1) ? 0 : 1;
		StartTick = (StartTick == 1) ? 0 : 1;
	}

	StopTick = GetTickCount();
	printf("Ternary condition: %d MS", StopTick - StartTick);
	return;
}
Код:
#include <a_samp>

main()
{
	new StartTick, StopTick;
	StartTick = GetTickCount();

	for(new i; i < 1000000; i ++)
	{
		if(StartTick == 1)
		{
			StartTick = 0;
		}
		else
		{
			StartTick = 1;
		}
		if(StartTick == 1)
		{
			StartTick = 0;
		}
		else
		{
			StartTick = 1;
		}
		if(StartTick == 1)
		{
			StartTick = 0;
		}
		else
		{
			StartTick = 1;
		}
		if(StartTick == 1)
		{
			StartTick = 0;
		}
		else
		{
			StartTick = 1;
		}
		if(StartTick == 1)
		{
			StartTick = 0;
		}
		else
		{
			StartTick = 1;
		}
	}

	StopTick = GetTickCount();
	printf("Normal condition: %d MS", StopTick - StartTick);
	return;
}
EACH BENCHMARK SHOULD BE DONE SEPARATELY.

Ternary conditions are little slower than normal conditions(if/else...).
Reply
#55

Quote:
Originally Posted by Yousha_Aleayoub
Посмотреть сообщение
lol. do you know about benchmarking? so do its yourself.
I never says anything without a reason/proof.

Normal condition: 10760068 MS
Ternary condition: 10955372 MS


Код:
...
Ternary conditions are little slower than normal conditions(if/else...).
You are changing the "StarTick" value.


Trys this:

pawn Код:
main()
{
    new start, StartTick, StopTick;
    start = GetTickCount();

    for(new i; i < 1000000; i ++)
    {
        StartTick = (StartTick == 1) ? 0 : 1;
        StartTick = (StartTick == 1) ? 0 : 1;
        StartTick = (StartTick == 1) ? 0 : 1;
        StartTick = (StartTick == 1) ? 0 : 1;
        StartTick = (StartTick == 1) ? 0 : 1;
    }

    StopTick = GetTickCount();
    printf("Ternary condition: %d MS", StopTick - start);

    start = GetTickCount();

    for(new i; i < 1000000; i ++)
    {
        if(StartTick == 1)
        {
            StartTick = 0;
        }
        else
        {
            StartTick = 1;
        }
        if(StartTick == 1)
        {
            StartTick = 0;
        }
        else
        {
            StartTick = 1;
        }
        if(StartTick == 1)
        {
            StartTick = 0;
        }
        else
        {
            StartTick = 1;
        }
        if(StartTick == 1)
        {
            StartTick = 0;
        }
        else
        {
            StartTick = 1;
        }
        if(StartTick == 1)
        {
            StartTick = 0;
        }
        else
        {
            StartTick = 1;
        }
    }

    StopTick = GetTickCount();
    printf("Normal condition: %d MS", StopTick - start);
}

[18/11/2012 08:44:14] Ternary condition: 133 MS
[18/11/2012 08:44:14] Normal condition: 159 MS
Reply
#56

@[J]ulian
WRONG TEST.
EACH BENCHMARK SHOULD BE DONE SEPARATELY.
like me.

After Ternary benchmark, CPU/RAM get involved.
as i said WRONG TEST.
Reply
#57

Quote:
Originally Posted by Yousha_Aleayoub
Посмотреть сообщение
@[J]ulian
lol. WRONG TEST.
EACH BENCHMARK SHOULD BE DONE SEPARATELY.
like me.

After Ternary benchmark, CPU/RAM get involved.
as i said WRONG TEST.
pawn Код:
#define LOOPS       (1000000)

public OnFilterScriptInit()
{
    new tmp = 0;
    new time = GetTickCount();
   
    for(new i = 0; i < LOOPS; ++i)
    {
        if(i > -1) {
            tmp = 1;
        }
        else {
            tmp = 2;
        }
    }
    printf("if else time == %dms", GetTickCount()-time);
   
    time = GetTickCount();//reset time
   
    for(new i = 0; i < LOOPS; ++i)
    {
        tmp = (i > -1)? 1:2;
    }
    printf("ternary time == %dms", GetTickCount()-time);
    return 1;
}
Output:
Код:
[12:19:41] if else time == 273ms
[12:19:42] ternary time == 266ms
More or less no difference at all over a million iterations.

EDIT: Result are also more or less identical when benched separately too (even smaller difference (3ms) for me).
Reply
#58

Well here's a tip that hopefully won't get deleted unlike what Yousha is claiming to be true... use Slice's benchmark code. Is a quite nice piece of code.
Reply
#59

Quote:
Originally Posted by Yousha_Aleayoub
Посмотреть сообщение
@[J]ulian
WRONG TEST.
EACH BENCHMARK SHOULD BE DONE SEPARATELY.
like me.

After Ternary benchmark, CPU/RAM get involved.
as i said WRONG TEST.
Your test is wrong, as you modify the starting time during the test, and so just get the current tick time as benchmark result. Thats also why you dont get proper results when running your both tests at once: the tick time is about the same so both tests seem to run with the same speed. The benchmark time of 10760068ms = 3 hours should already make the test suspicious.
If you still dont believe that your test doesnt work, try running it the other way round, the tenary test first, and then the if test.
Reply
#60

Quote:
Originally Posted by Mauzen
Посмотреть сообщение
Your test is wrong, as you modify the starting time during the test, and so just get the current tick time as benchmark result. Thats also why you dont get proper results when running your both tests at once: the tick time is about the same so both tests seem to run with the same speed. The benchmark time of 10760068ms = 3 hours should already make the test suspicious.
If you still dont believe that your test doesnt work, try running it the other way round, the tenary test first, and then the if test.
I didn't even notice that.

@Y_Less what I was talking about was this "Some corporate programming guidelines list the use of the conditional operator as bad practice because it can harm readability and long-term maintainability" - http://en.wikipedia.org/wiki/%3F: however as I now see needs a citation. I got slapped on my wrist once for using it in one of my assignments. (was too lazy to restructure the code so I used about three conditional operators in one line).
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)