Very slow writing to disk

I have this method where I write 360 lines to disk, and it takes 4040 milliseconds :

[code] system.DebugLog CurrentMethodName

dim previousmilli as int64 = Microseconds/1000

if diko <> nil then
dim f as folderItem = SpecialFolder.Applicationdata.child(App.AppDataFolder)
if not f.Exists then
f.CreateAsFolder
end if
system.debuglog "folderitem: "+ str((Microseconds/1000)-previousmilli)

previousmilli = Microseconds/1000
if f.Exists then
  f = f.child("Diko.csv")
  dim lines() as string
  if Diko.Count > 0 then
    For Each key As Variant In Diko.Keys
      lines.append(key+chr(9)+";"+tab+Diko.Value(key))
    Next
  end if
  system.debuglog "Dictionary keys: "+ str((Microseconds/1000)-previousmilli)
  
  previousmilli = Microseconds/1000
  dim linetowrite as string= join(lines,EndOfLine)
  system.debuglog "join lines: "+ str((Microseconds/1000)-previousmilli)
  
  previousmilli = Microseconds/1000
  Dim t As TextOutputStream
  If f <> Nil Then
    t = TextOutputStream.Create(f)
    t.Write(linetowrite)
    t.Close
  End If
end if
system.debuglog "write to disk: "+ str((Microseconds/1000)-previousmilli)
[/code]

Module1.SaveDiko folderitem: 0.382476 7:28:24 PM Dictionary keys: 9.077429 join lines: 0.255405 write to disk: 4040.234

My iMac 2011 has an SSD, so it should be blazing fast. Yet, 4 seconds for 360 lines is very significant. Does not seem normal.

What could explain such a snail pace ?

its confusing how you are checking F for nil and exists … if it exists then it can’t be nil so the NIL check is useless

Why go thru DIKO (a dictionary) to create a array of lines, then join those into a string
why not just write each line from the dictionary and forgo the array

if Diko.Count > 0 then
   For Each key As Variant In Diko.Keys
   t.writeline key+chr(9)+";"+tab+Diko.Value(key)
   Next
 end if

yes, I know that doesn’t explain the delay… but perhaps removing reduncacy might fix the issue

Actually, that was what I was doing before and I thought the delay was due to to writing each line, so I went to write a big chunk. As you can see above, creating the array from the dictionary takes only 9 milliseconds.

How big are the lines? (or the final file size)
I just tried your code on my mac (HD) and is really quicker in the write phase.

So:
A) your data is greater than my test data (probably)
B) you have some trouble on your SSD

[quote=370752:@Antonio Rinaldi]How big are the lines? (or the final file size)
I just tried your code on my mac (HD) and is really quicker in the write phase.

So:
A) your data is greater than my test data (probably)
B) you have some trouble on your SSD[/quote]

The final file size in 25K. Should not be such a problem.

I am going to check my SSD, but let alone on High Sierra startup, I would see immediately see if it had become as slow as a regular hard drive.

Is the disk HFS+ or APFS?

APFS.

C)… APFS

This may help to see how much time is the write and how much is creating the file:

previousmilli = Microseconds/1000
Dim t As TextOutputStream
If f <> Nil Then
 t = TextOutputStream.Create(f)
 system.debuglog "create file: "+ str((Microseconds/1000)-previousmilli)
 previousmilli = Microseconds/1000
 t.Write(linetowrite)
 system.debuglog "write file: "+ str((Microseconds/1000)-previousmilli)
 t.Close
End If

this is one thing I have found to be painfully slow

 system.debuglog

to the point I have a custom method using a standard textoutputstream that is many many times faster

OK. I had a hunch that it could be APFS. So I saved to a good old hard drive. 39 milliseconds !

I am going to reformat my drive to HFS+. This iOSized macOS is a shitty pile of manure :confused: