Jump to content

Recommended Posts

Posted

Hello, I have created functions similar to INI files, and I would like to know how to optimize the code. Do you have any ideas? Thank you.

 

Func _DbWrite_($db_file,$db_table,$db_key,$db_value)
    _SQLite_Startup()
    Local $db_query
    $db_open = _SQLite_Open($db_file)
    _SQLite_Exec($db_open,'CREATE TABLE IF NOT EXISTS "' & $db_table & '" (key,value);')
    _SQLite_QuerySingleRow($db_open,'SELECT key FROM "' & $db_table & '" WHERE key="' & $db_key & '";',$db_query)
    If $db_query[0] == "" Then
        _SQLite_Exec($db_open,'INSERT INTO "' & $db_table & '" VALUES ("' & $db_key & '","' & $db_value & '");')
    Else
        _SQLite_Exec($db_open,'UPDATE "' & $db_table & '" SET value="' & $db_value & '" WHERE key="' & $db_key & '";')
    EndIf
    _SQLite_Close($db_open)
    _SQLite_Shutdown()
EndFunc

Func _DbRead_($db_file,$db_table,$db_key,$db_value)
    _SQLite_Startup()
    Local $db_query
    $db_open = _SQLite_Open($db_file)
    _SQLite_Exec($db_open,'CREATE TABLE IF NOT EXISTS "' & $db_table & '" (key,value);')
    _SQLite_QuerySingleRow($db_open,'SELECT value FROM "' & $db_table & '" WHERE key="' & $db_key & '";',$db_query)
    _SQLite_Close($db_open)
    If $db_query[0] == "" Then
        Return $db_value
    Else
        Return $db_query[0]
    EndIf
    _SQLite_Shutdown()
EndFunc

 

Posted

Keep in mind the overhead with SQLite calls from AutoIt. You're calling the DLL to do stuff in SQL, get data back and do something with it. It's a lot easier to read an ini file. Here's your posted version comparison:

[================= Run progress, 10,000 times * 2 functions ===================]
 +———————————————+—————————————————+———————————————+——————————————+—————————————+
 | Function Name | Time Taken (ms) | Avg Time (ms) | Std Dev (ms) | Faster By % |
 +———————————————+—————————————————+———————————————+——————————————+—————————————+
+| IniRead       |          681.45 |        0.0681 |       0.0180 |       94.97 |
 | _DbRead_      |        13559.59 |        1.3560 |       0.2351 |        0.00 |
 +———————————————+—————————————————+———————————————+——————————————+—————————————+

And with @argumentum's suggestion of only doing [StartUp and Shutdown], [Open and Close] once:

[================= Run progress, 10,000 times * 2 functions ===================]        
 +———————————————+—————————————————+———————————————+——————————————+—————————————+
 | Function Name | Time Taken (ms) | Avg Time (ms) | Std Dev (ms) | Faster By % |
 +———————————————+—————————————————+———————————————+——————————————+—————————————+
+| IniRead       |          760.12 |        0.0760 |       0.0265 |       87.07 |
 | _DbRead_2     |         5878.42 |        0.5878 |       0.1161 |        0.00 |
 +———————————————+—————————————————+———————————————+——————————————+—————————————+

So, if all you're after is a couple of basic configuration options, I'd recommend sticking with a .ini file. If they're options that don't need to be constantly written/read, SQLite can work fine for getting options once at the start of the script, though you should probably create a function to get a whole 'section' of values at once, instead of one by one.

We ought not to misbehave, but we should look as though we could.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...