Sign in to follow this  
Followers 0
Venix

Help Me Speed Up Application

5 posts in this topic

#1 ·  Posted (edited)

A while ago I was working on a small application that I wanted to store information in a table format; I wanted it to be a light weight portable database that could be taken around on a memory stick. Problems began to occur with how I coded the load function of the application because it would read and Insert every line of an Ini file until the application returned nothing. This worked great when I was only dealing with lets say 100 entries however if I wanted to deal with a greater number of entries it would take an inefficient amount of time to load the data. The code of this application is unfinished however I would people to take a look at the loading functions of the program and tell me if I was to re write the script what could I do to make it faster.

Edit: Sorry if i don't reply until later I'm a little busy today but all feedback is appreciated. :D

; Customer Database Coded By Venix

; Include Required UDF's.
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiStatusBar.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <DateTimeConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <GuiEdit.au3>

; Start Of GUI Creation.
$DatabaseForm = GUICreate("Customer Database", 700, 523, -1, -1)
$Tools = GUICtrlCreateMenu("Tools")
$Remove = GUICtrlCreateMenuItem("Remove", $Tools)
$Help = GUICtrlCreateMenu("Help")
$Tutorial = GUICtrlCreateMenuItem("Tutorial", $Help)
$About = GUICtrlCreateMenuItem("About", $Help)
$Database = GUICtrlCreateListView("Row|Date|Customer ID|Forename|Surname|Telephone|Address", 8, 8, 684, 410, BitOR($LVS_REPORT,$LVS_NOSORTHEADER,$LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES))
$DatePicker = GUICtrlCreateDate(@YEAR & "/" & @WDAY & "/" & @MDAY & @Hour & ":" & @MIN & ":" & @SEC, 8, 448, 137, 21)
$ForenameInput = GUICtrlCreateInput("", 152, 448, 100, 21)
$SurnameInput = GUICtrlCreateInput("", 257, 448, 100, 21)
$TelephoneInput = GUICtrlCreateInput("", 361, 448, 100, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
$AddressInput = GUICtrlCreateInput("", 465, 448, 161, 21)
$DateLabel = GUICtrlCreateLabel("Select Date", 48, 426, 60, 17)
$ForenameLabel = GUICtrlCreateLabel("Enter Forename", 162, 426, 79, 17)
$SurnameLabel = GUICtrlCreateLabel("Enter Surname", 269, 426, 74, 17)
$TelephoneLabel = GUICtrlCreateLabel("Enter Telephone", 366, 426, 83, 17)
$AddressLabel = GUICtrlCreateLabel("Enter Address", 507, 426, 70, 17)
$DatabaseAdd = GUICtrlCreateButton("Add Data", 632, 445, 65, 25)
$DisplayStatus = _GUICtrlStatusBar_Create($DatabaseForm)
Dim $DisplayStatus_PartsWidth[3] = [100, 570, -1]
_GUICtrlStatusBar_SetParts($DisplayStatus, $DisplayStatus_PartsWidth)
_GUICtrlStatusBar_SetText($DisplayStatus, "Key: ", 0)
_GUICtrlStatusBar_SetText($DisplayStatus, "Data: ", 1)
_GUICtrlStatusBar_SetText($DisplayStatus, "Status: Idle", 2)
_GUICtrlStatusBar_SetMinHeight($DisplayStatus, 25)
GUISetState(@SW_SHOW)

; Setting the width of the listveiw columns.
_GUICtrlListView_SetColumnWidth($Database, 0, 60) ; Row Number Column Size.
_GUICtrlListView_SetColumnWidth($Database, 1, 70) ; Date Column Size.
_GUICtrlListView_SetColumnWidth($Database, 2, 78) ; Customer ID Column Size.
_GUICtrlListView_SetColumnWidth($Database, 3, 78) ; Forename Column Size.
_GUICtrlListView_SetColumnWidth($Database, 4, 78) ; Surname Column Size.
_GUICtrlListView_SetColumnWidth($Database, 5, 85) ; Telephone Column Size.
_GUICtrlListView_SetColumnWidth($Database, 6, 231) ; Address Column Size.
; End Of GUI Creation.

; Start Variables
$RowNum = ""
; End Variables

DatabaseCheck() ; Run Database Check Function On Startup.
LoadDatabase()

Func LoadDatabase() ; Function Will Load The Database
_GUICtrlStatusBar_SetText($DisplayStatus, "Status: Loading", 2)
GetRowNumber() ; Use this function to receive the row number
$RowNum = $RowNum -1
For $Key = 1 To $RowNum Step 1 ; Repeat step depending on how much data there is.
$RowTemp = IniRead(@ScriptDir & "Customer Database.ini", "Row", $Key, "Error") ; Read data in row section.
_GUICtrlStatusBar_SetText($DisplayStatus, "Key: " & $Key , 0)
_GUICtrlStatusBar_SetText($DisplayStatus, "Data: " & $RowTemp , 1)
$DateTemp = IniRead(@ScriptDir & "Customer Database.ini", "Date", $Key, "Error") ; Read data in date section.
_GUICtrlStatusBar_SetText($DisplayStatus, "Key: " & $Key , 0)
_GUICtrlStatusBar_SetText($DisplayStatus, "Data: " & $DateTemp , 1)
$CustomerIDTemp = IniRead(@ScriptDir & "Customer Database.ini", "CustomerID", $Key, "Error") ; Read data in customerid section.
_GUICtrlStatusBar_SetText($DisplayStatus, "Key: " & $Key , 0)
_GUICtrlStatusBar_SetText($DisplayStatus, "Data: " & $CustomerIDTemp , 1)
$ForenameTemp = IniRead(@ScriptDir & "Customer Database.ini", "Forename", $Key, "Error") ; Read data in forename section.
_GUICtrlStatusBar_SetText($DisplayStatus, "Key: " & $Key , 0)
_GUICtrlStatusBar_SetText($DisplayStatus, "Data: " & $ForenameTemp , 1)
$SurnameTemp = IniRead(@ScriptDir & "Customer Database.ini", "Surname", $Key, "Error") ; Read data in surname section.
_GUICtrlStatusBar_SetText($DisplayStatus, "Key: " & $Key , 0)
_GUICtrlStatusBar_SetText($DisplayStatus, "Data: " & $SurnameTemp , 1)
$TelephoneTemp = IniRead(@ScriptDir & "Customer Database.ini", "Telephone", $Key, "Error") ; Read data in telephone section.
_GUICtrlStatusBar_SetText($DisplayStatus, "Key: " & $Key , 0)
_GUICtrlStatusBar_SetText($DisplayStatus, "Data: " & $TelephoneTemp , 1)
$AddressTemp = IniRead(@ScriptDir & "Customer Database.ini", "Address", $Key, "Error") ; Read data in address section.
_GUICtrlStatusBar_SetText($DisplayStatus, "Key: " & $Key , 0)
_GUICtrlStatusBar_SetText($DisplayStatus, "Data: " & $AddressTemp , 1)
; Write Info To Database
GUICtrlCreateListViewItem($RowTemp & "|" & $DateTemp & "|" & $CustomerIDTemp & "|" & $ForenameTemp & "|" & $SurnameTemp & "|" & $TelephoneTemp & "|" & $AddressTemp, $Database)
Next
_GUICtrlStatusBar_SetText($DisplayStatus, "Status: Idle", 2)
_GUICtrlStatusBar_SetText($DisplayStatus, "Key: ", 0)
_GUICtrlStatusBar_SetText($DisplayStatus, "Data: ", 1)
EndFunc

Func DatabaseCheck() ; Function will check if the database exists
If Not FileExists("Customer Database.ini") Then ; If database does not exist then create database.
FileWrite(@ScriptDir & "Customer Database.ini", "[Row]" & @CRLF & "[Date]" & @CRLF & "[CustomerID]" & @CRLF & "[Forename]" & @CRLF & "[Surname]" & @CRLF & "[Telephone]" & @CRLF & "[Address]")
MsgBox(64, "Database Not Found", "Database was not found so a new one was created.")
EndIf
EndFunc

Func GetRowNumber() ; Function will receive the row number for the next row.
DatabaseCheck() ; Check the database before using a function that relys on it.
$CheckIni = IniRead(@ScriptDir & "Customer Database.ini", "Row", 1, "Error")
If $CheckIni = "Error" Then ; If no row information has been written then set row number to 1.
$RowNum = 1
Else
Do ; Add 1 to row number until an error is returned.
$RowNum = $RowNum + 1
$RowReturn = IniRead(@ScriptDir & "Customer Database.ini", "Row", $RowNum, "Error")
Until $RowReturn = "Error"
EndIf
EndFunc

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

Case $DatabaseAdd ; On button press.
GetRowNumber() ; Use Row number function.
If _GUICtrlEdit_GetText($ForenameInput) = "" Or _GUICtrlEdit_GetText($SurnameInput) = "" Or _GUICtrlEdit_GetText($TelephoneInput) = "" Or _GUICtrlEdit_GetText($AddressInput) = "" Then
MsgBox(64, "Empty Feilds", "Please fill in all the required feilds.") ; If any feilds are empty then display a message.
Else
_GUICtrlStatusBar_SetText($DisplayStatus, "Status: Writing", 2)
_GUICtrlStatusBar_SetText($DisplayStatus, "Key: ", 0)
IniWrite(@ScriptDir & "Customer Database.ini", "Row", $RowNum, $RowNum) ; Write the row number to database.
IniWrite(@ScriptDir & "Customer Database.ini", "Date", $RowNum, GuiCtrlRead($DatePicker, 0)) ; Write the date to the database.
IniWrite(@ScriptDir & "Customer Database.ini", "CustomerID", $RowNum, "Test ID") ; Write Customer ID to database, ID code is incomplete however not neccesary.
IniWrite(@ScriptDir & "Customer Database.ini", "Forename", $RowNum, _GUICtrlEdit_GetText($ForenameInput)) ; Write Forename to database.
IniWrite(@ScriptDir & "Customer Database.ini", "Surname", $RowNum, _GUICtrlEdit_GetText($SurnameInput)) ; Write Surname to database.
IniWrite(@ScriptDir & "Customer Database.ini", "Telephone", $RowNum, _GUICtrlEdit_GetText($TelephoneInput)) ; Write Telephone number to database.
IniWrite(@ScriptDir & "Customer Database.ini", "Address", $RowNum, _GUICtrlEdit_GetText($AddressInput)) ; Write Address to database.
;Add Item To ListVeiw
$DataWrite = GUICtrlCreateListViewItem ($RowNum & "|" & GuiCtrlRead($DatePicker, 0) & "|" & "Test ID" & "|" & _GUICtrlEdit_GetText($ForenameInput) & "|" & _GUICtrlEdit_GetText($SurnameInput) & "|" & _GUICtrlEdit_GetText($TelephoneInput) & "|" & _GUICtrlEdit_GetText($AddressInput), $Database)
_GUICtrlStatusBar_SetText($DisplayStatus, "Status: Idle", 2)
EndIf

Case $Remove ; Function will remove record from the database.
;Needs to be coded.
Case $Tutorial
MsgBox(64, "Tutorial", "")
Case $About
MsgBox(64, "About", "")
EndSwitch
WEnd
Edited by Venix

Share this post


Link to post
Share on other sites



SQLite seems to suit well. Search the forum for "SQLite UDF" in the example forum.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Adding the massive overhead of a database system would only result in drastically increased load times. It also throws a wrench into your wish to keep it "light weight and portable". I'd trash all the bells and whistles of the Ini functions and load a flat file in a single read.

Edit: PS - If there were ever a good candidate for a static variable, in your existing code, @ScriptDir & "Customer Database.ini" is it!

Edited by Spiff59

Share this post


Link to post
Share on other sites

SQLite seems to suit well. Search the forum for "SQLite UDF" in the example forum.

Thanks I'll check it out.

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0