View Single Post
Old 15/08/2012, 03:36 PM   #2726
AndreT
Gangsta
 
AndreT's Avatar
 
Join Date: Jul 2011
Posts: 966
Reputation: 414
Default Re: [REL] MySQL Plugin (R7 released)

Quote:
Originally Posted by xxmitsu View Post
@AndreT, good job. I guess, same thing you could do with floats instead of using floatstr in pawn.
Thanks for the feedback, I guess you're right!

I knew I was going to run into problems, but they solved more easily than I thought, and stay tuned for the speed results below.
Code:
// CScripting.cpp
cell AMX_NATIVE_CALL Natives::n_cache_get_row_float(AMX* amx, cell* params)
{
    unsigned int cID = params[3] - 1;
    Mutex::getInstance()->_lockMutex();
    VALID_CONNECTION_HANDLE("cache_get_row_float", cID);
    CMySQLHandler *cHandle = SQLHandle[cID];

    unsigned int a = params[1];
    if(a > cHandle->m_sCache.size())
        return 0;

    float value = (float)atof(cHandle->m_sCache[a][params[2]]);
    Mutex::getInstance()->_unlockMutex();
    return amx_ftoc(value);
}
Code:
// CScripting.h
class Natives
{
    // ...
    static cell AMX_NATIVE_CALL n_cache_get_row_float(AMX* amx, cell* params);
    // ...
};
Code:
// main.cpp
const AMX_NATIVE_INFO MySQLNatives[] = 
{
    // ...
    {"cache_get_row_float",		Natives::getInstance()->n_cache_get_row_float},
    // ...
};
pawn Code:
// a_mysql.inc
native Float:cache_get_row_float(row, idx, connectionHandle = 1);

At first I had problems because I passed a double to amx_ftoc and did not get the right results in PAWN, but resolved it with the cast.

The code I used for testing uses my own bunch of OO-like definitions, but I made a query select 6 floating point fields from the database and then parsed them a million times.
pawn Code:
for(new i = 0; i != 1000000; i++)
{
    cache_get_row(0, 0, str), tempFLOAT[0] = floatstr(str);
    cache_get_row(0, 1, str), tempFLOAT[1] = floatstr(str);
    cache_get_row(0, 2, str), tempFLOAT[2] = floatstr(str);
    cache_get_row(0, 3, str), tempFLOAT[3] = floatstr(str);
    cache_get_row(0, 4, str), tempFLOAT[4] = floatstr(str);
    cache_get_row(0, 5, str), tempFLOAT[5] = floatstr(str);
}
// Execution time: 9758ms
pawn Code:
for(new i = 0; i != 1000000; i++)
{
    tempFLOAT[0] = cache_get_row_float(0, 0);
    tempFLOAT[1] = cache_get_row_float(0, 1);
    tempFLOAT[2] = cache_get_row_float(0, 2);
    tempFLOAT[3] = cache_get_row_float(0, 3);
    tempFLOAT[4] = cache_get_row_float(0, 4);
    tempFLOAT[5] = cache_get_row_float(0, 5);
}
// Execution time: 4947

I was not initially thinking of a speed improvement, but convenience instead. Turns out, however, that this function and cache_get_row_int are quite a bit faster than using cache_get_row and then floatstr or strval.

Thanks.

Last edited by AndreT; 07/02/2013 at 03:12 PM.
AndreT is offline   Reply With Quote