SA-MP Forums Archive
Interesting Test - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Interesting Test (/showthread.php?tid=284822)



Interesting Test - dowster - 20.09.2011

pawn Код:
#include <a_samp>
#define ITERATIONS 100000000
main()
{
    new tc, tc1, tc2, tc3;
    tc = GetTickCount();
    for(new i = 0; i < ITERATIONS; i++) {
        new var = 5;
        var = (var / 5); }
    tc1 = (GetTickCount() - tc);
    tc = GetTickCount();
    for(new i = 0; i < ITERATIONS; i++) {
        new var = 5;
        var = (5 * ((1)/(5))); }
    tc2 = (GetTickCount() - tc);
    tc = GetTickCount();
    for(new i = 0; i < ITERATIONS; i++) {
        new var = 5;
        var = (5 * 0.2); }
    tc3 = (GetTickCount() - tc);
    printf(" Test 1: %i, Test 2: %i, Test 3: %i", tc1, tc2, tc3);
}
Result:
[15:42:49] Test 1: 11253, Test 2: 3135, Test 3: 18068


Why would this happen, is it just the way that the math portion of a computer CPU processes math?

P.S. If above is too hard to read:
pawn Код:
#include <a_samp>
#define ITERATIONS 100000000
main()
{
    new tc, tc1, tc2, tc3;
    tc = GetTickCount();
    for(new i = 0; i < ITERATIONS; i++)
    {
        new var = 5;
        var = (var / 5);
    }
    tc1 = (GetTickCount() - tc);
    tc = GetTickCount();
    for(new i = 0; i < ITERATIONS; i++)
    {
        new var = 5;
        var = (5 * ((1)/(5)));
    }
    tc2 = (GetTickCount() - tc);
    tc = GetTickCount();
    for(new i = 0; i < ITERATIONS; i++)
    {
        new var = 5;
        var = (5 * 0.2);
    }
    tc3 = (GetTickCount() - tc);
    printf(" Test 1: %i, Test 2: %i, Test 3: %i", tc1, tc2, tc3);
}



Re: Interesting Test - skullmuncher1337 - 20.09.2011

Your processor could have been busy doing something else at the same moment a certain test was done.


Re: Interesting Test - Babul - 20.09.2011

i guess its the script using a variable or a precompiled number.

test 1...
Код:
var = (var / 5);
... assigns 5 to var, and...
... calculates 1 as integer into var; its truncated if you divide any integer by an odd number. 3/2=0, 5/5=1

test 2...
Код:
var = (5 * ((1)/(5)));
... writes a precompiled number into var: (5*(1/5=0)) equals var=0;

test 3...
Код:
var = (5 * 0.2);
... writes the also precompiled, but this time a float i assume, as integer, into var. var=1.0000, as integer = 1?

test 2 proves thats its doing nothing but writing a cell into the vairable, while test1 and test3 are using the prior defined var.

repeat that test with public functions, using the Profiler Plugin ^^


Re: Interesting Test - dowster - 21.09.2011

Re-Tested, doing the whole lot of tests 25 times, and each test 1Million times per test instead of 100Million, so atotal of 25Million test per each method (i think).

Here's the code:
pawn Код:
#include <a_samp>
#define ITERATIONS 1000000
#define LOOPS 25
main()
{
    for(new l = 0; l < LOOPS; l++) {
        new tc, tc1, tc2, tc3;
        tc = GetTickCount();
        for(new i = 0; i < ITERATIONS; i++) {
            new var = 5;
            var = (var / 5); }
        tc1 = (GetTickCount() - tc);
        tc = GetTickCount();
        for(new i = 0; i < ITERATIONS; i++) {
            new var = 5;
            var = (var * ((1)/(5))); }
        tc2 = (GetTickCount() - tc);
        tc = GetTickCount();
        for(new i = 0; i < ITERATIONS; i++) {
            new var = 5;
            var = (var * 0.2); }
        tc3 = (GetTickCount() - tc);
        printf(" Test 1: %i, Test 2: %i, Test 3: %i", tc1, tc2, tc3); }
}
and the log output
Quote:
Originally Posted by My Log Files
[19:51:17] Test 1: 123, Test 2: 74, Test 3: 195
[19:51:17] Test 1: 120, Test 2: 71, Test 3: 182
[19:51:18] Test 1: 122, Test 2: 69, Test 3: 176
[19:51:18] Test 1: 123, Test 2: 69, Test 3: 175
[19:51:18] Test 1: 123, Test 2: 69, Test 3: 180
[19:51:19] Test 1: 126, Test 2: 65, Test 3: 180
[19:51:19] Test 1: 120, Test 2: 70, Test 3: 182
[19:51:19] Test 1: 123, Test 2: 65, Test 3: 182
[19:51:20] Test 1: 122, Test 2: 65, Test 3: 185
[19:51:20] Test 1: 121, Test 2: 70, Test 3: 178
[19:51:20] Test 1: 125, Test 2: 66, Test 3: 184
[19:51:21] Test 1: 128, Test 2: 66, Test 3: 182
[19:51:21] Test 1: 120, Test 2: 70, Test 3: 195
[19:51:22] Test 1: 173, Test 2: 91, Test 3: 187
[19:51:22] Test 1: 124, Test 2: 74, Test 3: 177
[19:51:22] Test 1: 130, Test 2: 68, Test 3: 195
[19:51:23] Test 1: 142, Test 2: 68, Test 3: 186
[19:51:23] Test 1: 125, Test 2: 70, Test 3: 187
[19:51:24] Test 1: 125, Test 2: 72, Test 3: 180
[19:51:24] Test 1: 122, Test 2: 68, Test 3: 188
[19:51:24] Test 1: 134, Test 2: 65, Test 3: 196
[19:51:25] Test 1: 128, Test 2: 66, Test 3: 185
[19:51:25] Test 1: 124, Test 2: 70, Test 3: 181
[19:51:26] Test 1: 126, Test 2: 76, Test 3: 196
[19:51:26] Test 1: 129, Test 2: 67, Test 3: 186
P.S. I'll try using random values for the variables now

Edit: makes much more sense now
pawn Код:
#include <a_samp>
#define ITERATIONS 1000000
#define LOOPS 25
main()
{
    for(new l = 0; l < LOOPS; l++) {
        new tc, tc1, tc2 /* , tc3 */;
        tc = GetTickCount();
        for(new i = 0; i < ITERATIONS; i++) {
            new var = random(10) + 1;
            var = (var / 5); }
        tc1 = (GetTickCount() - tc);
        tc = GetTickCount();
        for(new i = 0; i < ITERATIONS; i++) {
            new var = random(10) + 1;
            var = (var * ((1)/(var))); }
        tc2 = (GetTickCount() - tc);
        /* tc = GetTickCount();
        for(new i = 0; i < ITERATIONS; i++) {
            new var = 5;
            var = (var * 0.2); }
        tc3 = (GetTickCount() - tc); */

        printf(" Test 1: %i, Test 2: %i", tc1, tc2); }
}
And of course the log output

Quote:
Originally Posted by Log Files
[19:58:39] Test 1: 172, Test 2: 181
[19:58:39] Test 1: 193, Test 2: 190
[19:58:39] Test 1: 182, Test 2: 198
[19:58:40] Test 1: 185, Test 2: 200
[19:58:40] Test 1: 187, Test 2: 189
[19:58:40] Test 1: 194, Test 2: 190
[19:58:41] Test 1: 184, Test 2: 193
[19:58:41] Test 1: 191, Test 2: 192
[19:58:42] Test 1: 181, Test 2: 186
[19:58:42] Test 1: 192, Test 2: 204
[19:58:42] Test 1: 186, Test 2: 187
[19:58:43] Test 1: 180, Test 2: 187
[19:58:43] Test 1: 181, Test 2: 272
[19:58:44] Test 1: 208, Test 2: 205
[19:58:44] Test 1: 187, Test 2: 194
[19:58:44] Test 1: 195, Test 2: 206
[19:58:45] Test 1: 188, Test 2: 192
[19:58:45] Test 1: 186, Test 2: 196
[19:58:46] Test 1: 187, Test 2: 191
[19:58:46] Test 1: 202, Test 2: 198
[19:58:46] Test 1: 191, Test 2: 193
[19:58:47] Test 1: 186, Test 2: 193
[19:58:47] Test 1: 193, Test 2: 206
[19:58:47] Test 1: 188, Test 2: 193
[19:58:48] Test 1: 185, Test 2: 191
Going to code in average calculations


Re: Interesting Test - dowster - 21.09.2011

See the edited stuff ******, now it's making more sense. I had momentarily forgotten about the pre processor/pre compiler


Re: Interesting Test - GrimR - 21.09.2011

It's always good to experiment with this kind of thing, where more than one option to perform the same task exists.

It usually boils down to how badly you want that extra speed and efficiency and whether you are willing to sacrifice readability (may not always apply but often does).

For example, in Object Pascal the average programmer utilizes functions built on top of the provided file access functions, or use the default simpler ones. One day I was trying to make a file conversion project for very big data conversion (I'm talking megabytes to gigabytes of data), and what I did was dig right down to the languages most raw access and found that I could work with it, and also more raw routines for manipulation (with many tests) and turned several minutes of conversion into several seconds!

A massive difference, and I learned quite a bit in the process.

On a side note you should probably test with and without compiler optimizations on.


Re: Interesting Test - GrimR - 21.09.2011

When I say always good, I mean for learning purposes.

You will find that something as simple as calling code from a function, as opposed to directly including that code can cause significant delays (where heavily looped processing is involved at least). But if this same block of code is needed many times, it's no longer realistic to include it directly.

In the end it's up to you, experimenting like this is great for learning and sometimes you can find some amazing results, but as has been said it boils down to an old saying, if it ain't broke then don't fix it! .