File generation and writing, very slow

General FreeBASIC programming questions.
cl4551f13d
Posts: 5
Joined: Jun 01, 2020 1:22

File generation and writing, very slow

Postby cl4551f13d » Jun 03, 2020 6:37

Hello everyone.
I want to make an application that shows me the speed of writing and reading from a pendrive.

I have written in following program, but I notice that it is very slow. Writing 10MB to drive c: takes 77 seconds.

Code: Select all

DIM AS INTEGER FileHandler, ByteCounter
DIM AS Double dt
DIM AS integer ms
Dim AS integer Size
FileHandler = FREEFILE

print "Writing 10MB to the hard drive."
print "please wait..."

OPEN "c:\file.txt" FOR BINARY AS #FileHandler

    Size = 1048576 * 10
   
    dt=Timer
    FOR ByteCounter = 0 TO Size ' 1048575
        PUT #FileHandler, ByteCounter, "X"
    NEXT ByteCounter 
    dt = (Timer-dt)
   
    'Double to integer
    ms = dt
   
    print"Time elapsed: " & ms & " secs"   
CLOSE #FileHandler

Sleep


If I use a commercial program on the same computer, he writes at 20MB per second.

What is the problem with my program?

Note: I am a novice in this language. It is my first program.
PaulSquires
Posts: 882
Joined: Jul 14, 2005 23:41

Re: File generation and writing, very slow

Postby PaulSquires » Jun 03, 2020 10:29

Write the data in chunks (say, 4096 bytes) rather than one integer at a time. The overhead from all those PUT calls is slowing down the process.
marcov
Posts: 3010
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: File generation and writing, very slow

Postby marcov » Jun 03, 2020 11:26

And never use position arguments if the writes are sequential. Besides the write() that every put does, that adds a lseek.
dodicat
Posts: 6687
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: File generation and writing, very slow

Postby dodicat » Jun 03, 2020 12:38

Do it in one.

Code: Select all

 

#include "file.bi"
Sub savefile(filename As String,p As String)
  Dim As Integer n
  n=Freefile
  If Open (filename For Binary Access Write As #n)=0 Then
    Put #n,,p
    Close
  Else
    Print "Unable to save " + filename
  End If
End Sub

dim as string s
dim as double t

t=timer
s=string(1048576 * 10,"x")
print "data length ";len(s)/1000000;" megabytes"
savefile("file.txt",s)
print cbool(fileexists("file.txt")),"  The file has been created"
print "file size ";filelen("file.txt")/1000000;" megabytes"
print "Time taken ";timer-t
print "Press a key. . ."
sleep
kill "file.txt"
print iif(fileexists("file.txt")=0,"OK file deleted","Delete the file manually")
print "Press a key to end . . ."
sleep

 

.05 seconds here.
badidea
Posts: 2148
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: File generation and writing, very slow

Postby badidea » Jun 03, 2020 16:24

cl4551f13d wrote:I want to make an application that shows me the speed of writing and reading from a pendrive.

This can be difficult. The OS may cache things and you can end up measuring completely different things. If the USB-stick is still flashing (if it has a led) after the test, then don't trust the result. Also virus-scanners can slow down the results dramatically.
cl4551f13d
Posts: 5
Joined: Jun 01, 2020 1:22

Re: File generation and writing, very slow

Postby cl4551f13d » Jun 04, 2020 4:26

dodicat wrote:Do it in one.

Code: Select all

 

#include "file.bi"
Sub savefile(filename As String,p As String)
  Dim As Integer n
  n=Freefile
  If Open (filename For Binary Access Write As #n)=0 Then
    Put #n,,p
    Close
  Else
    Print "Unable to save " + filename
  End If
End Sub

dim as string s
dim as double t

t=timer
s=string(1048576 * 10,"x")
print "data length ";len(s)/1000000;" megabytes"
savefile("file.txt",s)
print cbool(fileexists("file.txt")),"  The file has been created"
print "file size ";filelen("file.txt")/1000000;" megabytes"
print "Time taken ";timer-t
print "Press a key. . ."
sleep
kill "file.txt"
print iif(fileexists("file.txt")=0,"OK file deleted","Delete the file manually")
print "Press a key to end . . ."
sleep

 

.05 seconds here.


It works very well.

Thank you for the time you have invested in helping me.

Thanks a lot.

Return to “General”

Who is online

Users browsing this forum: No registered users and 7 guests