Does IniWrite rewrite the whole file every time? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
HakitoJin
Posts: 17
Joined: 02 Oct 2016, 17:20

Does IniWrite rewrite the whole file every time?

10 Aug 2018, 07:07

Because I got like 50 IniWrites and I am thinking FileAppend would be faster.
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Does IniWrite rewrite the whole file every time?

10 Aug 2018, 07:57

It depends on how you will be reading those values later on, it you read them individual then IniWrite might be better but if you end up reading the file in a loop then doing a FileAppend would be better since you only write one thing. Just note with file append you have to delete the file before appending it otherwise the old text will stay. With IniWrite you only override the old key values. You could also use FileOpen() with the w flag to prevent yourself from doing the FileDelete FileAppend method.
HakitoJin
Posts: 17
Joined: 02 Oct 2016, 17:20

Re: Does IniWrite rewrite the whole file every time?

10 Aug 2018, 08:55

Right, but isn't IniWrite just "FileRead FileDelete FileAppend" over and over?
If it doesn't do anything special then I'd rather format my stuff to look like an ini, use FileAppend to save it, and then IniRead to read pieces of it.

Hm, I'll go to test if IniWrite is faster than FileAppend, if it rewrites everything to update the file, then it should be obvious by how slow it is.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Does IniWrite rewrite the whole file every time?

10 Aug 2018, 09:27

AHK uses the WritePrivateProfileString function.
I'm not sure exactly how it works, but I do not think it rewrites the whole file, but it just rewrites everything starting from the key that is going to be modified.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Does IniWrite rewrite the whole file every time?

10 Aug 2018, 09:40

i wouldnt bother with ini. use the json lib instead, saving and reading is just 2 calls away
HakitoJin
Posts: 17
Joined: 02 Oct 2016, 17:20

Re: Does IniWrite rewrite the whole file every time?

10 Aug 2018, 10:02

In my experiment IniWrite was about 2 times faster than FileAppend, but only when saving a single key with IniWrite and 30 with FileAppend.
If you save 30 keys with both, then IniWrite becomes up to TEN TIMES SLOWER than FileAppend, holy guacamole!


Stats:
30 Keys with FileAppend = 0.5-1.0ms / 1640-3236 computations
30 Keys with IniWrite = 7.2-10.8ms / 22640-33964 computations
1 Key with IniWrite = 0.4-0.5ms / 1392-1578 computations


Code used for testing:

Code: Select all

ListLines OFF					;Doesn't display the script lines most recently executed, thus improving performance
;#KeyHistory 0					;KeyHistory is required by A_PriorKey, so don't disable it!
#InstallKeybdHook				;Ensures that hotkeys are more responsive
#InstallMouseHook				;Ensures that  hotkeys are more responsive
#UseHook ON					;Ensures that  hotkeys are more responsive
#SingleInstance FORCE
#NoEnv						;Avoids checking empty variables to see if they are environment variables
#Warn UseEnv
#MaxThreadsBuffer OFF 			;Causes some or all hotkeys to buffer rather than ignore keypresses when their #MaxThreadsPerHotkey limit has been reached
#HotkeyInterval 90000000
#MaxHotkeysPerInterval 90000000	;Along with #HotkeyInterval, specifies the rate of hotkey activations beyond which a warning dialog will be displayed.
#MaxThreadsPerHotkey 255		;This is the max
#MaxThreads 255				;This is the max
#MaxMem 128					;Default is 64 megabytes max variable size
Process Priority,,High			;Sets the script's priority to High, it is advised that you never set it to "Real Time"
DetectHiddenWindows ON
SetTitleMatchMode 3			;A window's title must exactly match WinTitle to be a match
SetBatchLines -1				;Determines how fast the script will run, a value of -1 allows it to use up to 100% of a CPU core, however, Autohotkey can only use one CPU core - https://autohotkey.com/board/topic/22889-using-more-than-50-of-the-cpu/?p=262950
SetKeyDelay -1,-1
SetMouseDelay -1
SetDefaultMouseSpeed 0
SetWinDelay 0					;Although a delay of -1 (no delay at all) is allowed, it is recommended that at least 0 be used, to increase confidence that the script will run correctly even when the CPU is under load
SetControlDelay -1				;Sets the delay that will occur after each control-modifying command.
SetWorkingDir % A_ScriptDir
;^ Before adding these settings, saving 30 keys with IniWrite would consistently spike up to 30ms / 100000 computations, I am not sure which setting is responsible for fixing that, nor do I care.

Random,RandomValue,0,999

IniValues=
(
[General]
Entry1=837
Entry2=837
Entry3=837
Entry4=837
Entry5=837
Entry6=837
Entry7=837
Entry8=837
Entry9=837
Entry10=837
Entry11=837
Entry12=837
Entry13=837
Entry14=837
Entry15=837
Entry16=837
Entry17=837
Entry18=837
Entry19=837
Entry20=837
Entry21=837
Entry22=837
Entry23=837
Entry24=837
Entry25=837
Entry26=837
Entry27=837
Entry28=837
Entry29=837
Entry30=837
)


Test()
Loop 30
	IniWrite,%RandomValue%,Testing IniWrite Speed.ini,General,Entry%A_Index%
Test("Msgbox")


/*
Test()
FileDelete,Testing IniWrite Speed2.ini
FileAppend,%IniValues%,Testing IniWrite Speed2.ini
Test("Msgbox")
 */
ExitApp

Test(Message:="Tooltip") ;by Ħakito
{
	Static StartTime
	Static LastResult
	iF StartTime
	{
		DllCall("QueryPerformanceCounter", "Int64*", EndTime)  ;A_TickCount and MSec could also be used instead
		DllCall("QueryPerformanceFrequency", "Int64*", Frequency) ;The number of cpu cycles per second
		Result:=EndTime-StartTime " total computations`nWhich took " (EndTime-StartTime)/(Frequency/1000) "ms" LastResult
		LastResult:="`n`nLast result: " EndTime-StartTime " & " (EndTime-StartTime)/(Frequency/1000)
		StartTime:=""
		iF Message=Tooltip
			Tooltip % Result
		else iF Message=Msgbox
			Msgbox % Result
		return EndTime-StartTime
	}
	else DllCall("QueryPerformanceCounter","Int64*",StartTime)
}

F5::Reload
So in conclusion, IniWrite probably does rewrite the whole darn file.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Does IniWrite rewrite the whole file every time?  Topic is solved

10 Aug 2018, 10:11

Well, in your test it is obvious that using FileAppend you write everything at once, and on the contrary, you are calling IniWrite 30 times.
You can write everything at once:

Code: Select all

IniValues=
(
Entry1=837
Entry2=837
Entry3=837
Entry4=837
Entry5=837
Entry6=837
Entry7=837
Entry8=837
Entry9=837
Entry10=837
Entry11=837
Entry12=837
Entry13=837
Entry14=837
Entry15=837
Entry16=837
Entry17=837
Entry18=837
Entry19=837
Entry20=837
Entry21=837
Entry22=837
Entry23=837
Entry24=837
Entry25=837
Entry26=837
Entry27=837
Entry28=837
Entry29=837
Entry30=837
)
IniWrite % IniValues, % A_Desktop . "\~tmp.ini", General
HakitoJin
Posts: 17
Joined: 02 Oct 2016, 17:20

Re: Does IniWrite rewrite the whole file every time?

10 Aug 2018, 10:21

Flipeador wrote:Well, in your test it is obvious that using FileAppend you write everything at once, and on the contrary, you are calling IniWrite 30 times.
You can write everything at once:

Code: Select all

IniValues=
(
Entry1=837
Entry2=837
Entry3=837
Entry4=837
Entry5=837
Entry6=837
Entry7=837
Entry8=837
Entry9=837
Entry10=837
Entry11=837
Entry12=837
Entry13=837
Entry14=837
Entry15=837
Entry16=837
Entry17=837
Entry18=837
Entry19=837
Entry20=837
Entry21=837
Entry22=837
Entry23=837
Entry24=837
Entry25=837
Entry26=837
Entry27=837
Entry28=837
Entry29=837
Entry30=837
)
IniWrite % IniValues, % A_Desktop . "\~tmp.ini", General
Oh nice, didn't know it could be done like that!
Now both IniWrite and FileAppend have the same writing speed (the former is actually faster, woop woop :v), and therefore my issue is solved.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 379 guests