Home>
Question
When appending text to a CSV file, is there a difference in speed between Set and Close inside and outside the loop?
Code example Set and close in a loopTo narrow the scope of a variable
Const strCSVPath = "hoge.csv"
For Each str In Array ("a", "b", "c")
Dim FSO: Set FSO = CreateObject ("Scripting.FileSystemObject")
Dim TS: Set TS = FSO.OpenTextFile (strCSVPath, 8)
TS.WriteLine str
TS.Close
Next
Set and close outside the loop
If I want to reduce the number of Set and Close
Const strCSVPath = "hoge.csv"
Dim FSO: Set FSO = CreateObject ("Scripting.FileSystemObject")
Dim TS: Set TS = FSO.OpenTextFile (strCSVPath, 8)
For Each str In Array ("a", "b", "c")
TS.WriteLine str
Next
TS.Close
-
Answer # 1
-
Answer # 2
Processing that does not need to be performed inside the loop will be executed outside the loop because the amount of instructions to be executed is reduced.
This is also the case. Moreover, since the process includes disk access, the impact is relatively large.
However, as in this example, there is no significant difference in about 3 loops.
Related articles
- sql server - is there a difference in speed when squeezing conditions first or later during sql join?
- vba:do loop + i want to speed up the macro that copies and pastes each line
- database - difference in response speed between cloud db and local db
- vba - i want to speed up the operation of the access link table with a pass-through query
- want to determine if there is bold in vba string
- vba - speed up when moving old data to another sheet
- there is a difference between the value added in excel and the value directly added
- vba - i want to display a message box if there is an unentered part so that the macro is not executed
- vbscript - how to prevent error even if there is no network connection during vbs http communication
- is there a processing difference between adding 1 and -1 to a c language int type variable so that it has 0,1 and assigning 0,1?
- [rspec] i want to select the select box, but is there a difference between select() and select_tag()?
- take the second from the last line of excel vba there is a space
- python - difference in execution speed (program executed at a specified time)
- vba - i want to display the time difference using the time function
- i want to speed up vba pdf output processing
- i want to insert columns one by one with vba, but if there is a smart way please tell me
- vbscript - is there a way to get the return value by launching an application created in vbnet from vbsctipt?
- conditional branch when there is no deletion record in excel vba
- how to skip when there is a blank cell during processing with vba macro
- i don't understand the difference in range acquisition between vba "selection and select"
Yes.
If you set and close in the loop,
Scripting.FileSystemObject
is generated (file open), written, and closed three times.If you set or close outside the loop,
Scripting.FileSystemObject
is generated (file open) once, written three times, and closed once.It is rough, but the former requires 9 steps, while the latter requires 5 steps.
Also, creating objects (creating
Scripting.FileSystemObject
) and opening and closing files are expensive (slow).