Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-03

blockread/write?

14 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Preben Mikael Bohn

Hi all, I'm trying to use blockread/write to speed up a very inefficient unit I wrote some time ago.
From the documentation (
http://www.freepascal.org/docs-html/ref/refsu75.html#x159-16500013.3 ) it does not seem that I can be sure that "Count" records are read using blockread. Since my data should be read to a buffer I would like to do something like:

  count := MAX_BUFFER_SIZE;
  reached_index := 1;
  repeat
  begin
    blockread(fout, (pointer to buffer[reached_index]), count, actual_count);
    dec(count, actual_count);
    inc(reached_index, actual_count);
  end;
  until ((count = 0) or (err  0));

Where I'm not sure how to semantically write (pointer to buffer[reached_index]). Any ideas?

Best regards Preben

Reply by email

Preben Mikael Bohn
to Anton Tichawa: Hello, Preben! There are several other things too in this old procedure, that might still…
Anton Tichawa wrote
There are several other things too in this old procedure, that might still cause errors or need optimization. If you want, I'll write a small procedure that does the same, andor rewrite your procedure for comparison. That'd be for God's Sake, I don't need no money or any other thing.

I ended up assuming that it could always read "count" records (except of course in the end) and implemented that. It seems to work fine now, but of course it is not safe as the documentation does not specifically state that it will always do it this way... My program now runs 35 times faster than before... :-)

Best regards Preben

In politics stupidity is not a handicap - Napoleon

Reply by email

Preben Mikael Bohn
to Anton Tichawa: Hello, Preben! Safety First! Well, safety is still important. You should use the returned…
Anton Tichawa wrote
Well, safety is still important. You should use the returned count in your procedure to be on the safe soide. The end might come sooner than expected.

Right now I terminate the program if all data can not be read in one go and I have not have any problems with it. However you are completely right that I should use the returned count, the problem is that I don't know how to tell blockread to start from somewhere within my buffer-array and from the start. Of course I could just copy the buffer-content to another array, but this seems awful clumpsy.

Best regards Preben

Reply by email

Ivan Montes
to Preben Mikael Bohn

----- Original Message -----
From: "Preben Mikael Bohn"

Right now I terminate the program if all data can not be read in one go

and I

have not have any problems with it. However you are completely right that

I

should use the returned count, the problem is that I don't know how to

tell

blockread to start from somewhere within my buffer-array and from the

start.

Of course I could just copy the buffer-content to another array, but this seems awful clumpsy.
var  buffer :pByte;

or

type
  tbuffer = array [0..0] of byte;
  pbuffer = ^tbuffer;
var
  buffer :pBuffer;
blockread( f, buffer[ integer index starting at 0 ], desired_bytes_to_read,
actual_bytes_readed );

if you don't want to redefine the type of "buffer" you can type-cast it when calling blockread
i.e: pByte( buffer ) [ index ]

anyway I don't see what you want to do with that procedure, since you set a maximum value for the buffer but you don't check if the file is larger than the buffer. Recursive calls will overwrite the contents of the buffer.

ciao

Reply by email

Preben Mikael Bohn
to Ivan Montes
Ivan Montes wrote 6 lines
blockread( f, buffer[ integer index starting at 0 ], desired_bytes_to_read, actual_bytes_readed ); if you don't want to redefine the type of "buffer" you can type-cast it when calling blockread i.e: pByte( buffer ) [ index ]

OK, thanks, I'll try that.

anyway I don't see what you want to do with that procedure, since you set a maximum value for the buffer but you don't check if the file is larger than the buffer. Recursive calls will overwrite the contents of the buffer.

I know that the file is larger at all times (since that is what my program do).

Best regards Preben

Reply by email

Preben Mikael Bohn
to Anton Tichawa: Please post the data declarations (e. g. var Count: Integer; const MAX_BUFFER_SIZE = ..…
Anton Tichawa wrote
Please post the data declarations (e. g. var Count: Integer; const MAX_BUFFER_SIZE = .. etc.). Normally, an index starts from 0 upwards, e. g. if you had:

It starts at 1, but that really shouldn't matter that much.

If you really want the other stuff I can give you what I have now, but that is not really readable :-) :

const
  FILEAR_MAX_BUFFER_SIZE = 500;
...
  if startBufferIndex < realFileSize then
  begin
    {$i-}
    seek(dataFile, startBufferIndex);
    {$i+}
    err := IOResult;
    { xxx error check here deleted }
    count := min(endBufferIndex, realFileSize - 1) - startBufferIndex + 1;
    {$i-}
    blockread(dataFile, fabuffer, count);
    {$i+}
    err := IOResult;
    { xxx error check here deleted }
  end;

As you can see I simply assume that everything is read in to the buffer and I also know that the remaining file is larger than or equal to the count.

Best regards Preben

Reply by email

Anton Tichawa
to Preben Mikael Bohn: Hi all, I'm trying to use blockread/write to speed up a very inefficient unit I wrote…

Hello!

At first glance I saw several possible errors that might make the program crash. The until-condition contains "= 0" and should possibly read "<= 0"; and your first storage has index 1, which should possibly read 0 instead of 1.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Reply by email

Anton Tichawa
to Preben Mikael Bohn: Hi all, I'm trying to use blockread/write to speed up a very inefficient unit I wrote…

Hello, Preben!

There are several other things too in this old procedure, that might still cause errors or need optimization. If you want, I'll write a small procedure that does the same, andor rewrite your procedure for comparison. That'd be for God's Sake, I don't need no money or any other thing.

Hope ths helps,

Anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Reply by email

Mark Emerson
to Anton Tichawa: Hello! At first glance I saw several possible errors that might make the program crash.…

What program?

Mark

Anton Tichawa wrote 26 lines
Hello! At first glance I saw several possible errors that might make the program crash. The until-condition contains "= 0" and should possibly read "<= 0"; and your first storage has index 1, which should possibly read 0 instead of 1. ---------- "Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben." Doris Langley Moore: Ada, Countess of Lovelace (London 1977). ---------- Anton Tichawa Volkertstrasse 19 / 20 A-1020 Wien mobil: +43 664 52 07 907 email: ---------- _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Anton Tichawa
to Preben Mikael Bohn: I ended up assuming that it could always read "count" records (except of course in the…

Hello, Preben!

Safety First!

you wrote 13 lines
Anton Tichawa wrote: > There are several other things too in this old procedure, that might > still cause errors or need optimization. If you want, I'll write a small > procedure that does the same, andor rewrite your procedure for > comparison. That'd be for God's Sake, I don't need no money or any other > thing. I ended up assuming that it could always read "count" records (except of course in the end) and implemented that. It seems to work fine now, but of course it is not safe as the documentation does not specifically state that it will always do it this way... My program now runs 35 times faster than before... :-)

Well, safety is still important. You should use the returned count in your procedure to be on the safe soide. The end might come sooner than expected.

So, to be on the safe side, for a little tradeoff, include the returned "count" value in your algorithm, which will make your program womewhat slower, but still faster than it was before.

Will you run the program somwhere in some safe environment? Because there might still be errors in it .. I didn't have the time yet to read it thouroughly.

Bye Anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Reply by email

Anton Tichawa
to Preben Mikael Bohn: Right now I terminate the program if all data can not be read in one go and I have not…
you wrote 12 lines
Anton Tichawa wrote: > Well, safety is still important. You should use the returned count in > your procedure to be on the safe soide. The end might come sooner than > expected. Right now I terminate the program if all data can not be read in one go and I have not have any problems with it. However you are completely right that I should use the returned count, the problem is that I don't know how to tell blockread to start from somewhere within my buffer-array and from the start. Of course I could just copy the buffer-content to another array, but this seems awful clumpsy.

Please post the data declarations (e. g. var Count: Integer; const MAX_BUFFER_SIZE = .. etc.). Normally, an index starts from 0 upwards, e. g. if you had:

const MAX_BUFFER_SIZE = 8;

then index runs from 0 up to 7. When you want to access the first array entry, you use index 0; when you want to access the nth entry (e. g. n = 4), you use index n - 1 (e. g. 4 - 1 = 3).

This direct indexing when writing to or reading from the buffer will save resources, such as cpu time, memory, power, ..; and it makes your program overall faster. It might increase safety, too, because copying too many times by itself might be erroneous due to hardware failures.

Anton Tichawa.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Reply by email

Anton Tichawa
to Mark Emerson: What program? Mark

Well, unless it's your only chance (walking through the forests alone in the dark), you should'n start any program that might contain fatal errors. safety first, or so ..

bye,

anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Reply by email

Jonas Maebe
to Kenneth Due:
Kenneth Due wrote 14 lines
<html> I have a problem reading a unix-socket <br> <br> this snippet (from www.lirc.org) works, but it's in perl ........... :-(<br> <br> <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fd=socket(AF_UNIX,SOCK_STREAM,0);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(fd==-1)&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp; perror(&quot;socket&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;

[snip]

Please do not post html-only mails to this list, the above is what it looks like for people that use a mail client which doesn't understand html. See <http://www.eudora.com/techsupport/kb/908hq.html> for information on how to change this.

Thanks,

Jonas
FPC mailing lists admin

Reply by email