🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.pascal.borland
Article #418

Re: Multiple file open/close anomaly?

#418
From: Pedt Scragg
Date: Sun, 06 Dec 1998 00:00
88 lines
3487 bytes
Robert AH Prins [mailto:prinsra@williscorroon.com] decided it was an
auspicious moment for:
>Take this snippet:
>
>const
>  sfile: integer = -1;
>
>procedure open(_file: integer);
>begin
>{$ifdef fast}
>  if _file <> sfile then
>    begin;
>      sfile:= _file;
>{$endif}
>      close(datatout);
>      assign(datatout, files[_file]);
>      settextbuf(datatout, databuf^, 8192);
>
>      if __first_event then
>        begin
>          rewrite(datatout);
>          __first_event:= false;
>        end
>      else
>        append(datatout);
>{$ifdef fast}
>    end;
>{$endif}
>end; {open}
>
>It's used a program in which many procedures can write their results to one
>of two files (a per event file and a summary file). Going through the
>program, I suddenly realized that I had never tracked calls to "open", and
>much to my surprise I discovered that on several occasions it's called with
>the same value for _file on several consecutive invovations. So I added the
>conditionals, and timed again, clean DOS, SMARTDRV with write caching, and
>running both the old and the new versions several times in alternating
>succession. Much to my surprise the old version, which performs the code
>dozens of times more than the the new version, is actually faster! (NB: The
>file is initially opened for summary data in the main program)
>
>Does anyone have an explanation?
>
I initially assumed that you were opening the file if actually needed.
However, re-reading your code with the {$ifdef} more carefully, I see
that the extra code is not actually doing anything that affects the
process so the code is probably a bit slower just due to the extra bits
you've added.

I am assuming the fast option is to prevent reopening the same file and
I have assumed that one of the files will already be open when this
procedure is called. If there is no open file then perhaps you need a
procedure to do the first open that can be separate from this one.

If you change the code to:

const
  sfile: integer = -1;

procedure open(_file: integer);
begin
  if _file <> sfile then
    begin
      sfile:= _file;            

{only close then assign and rewrite/append the file if different to old
one - otherwise you do not actually need to do anything so the if..then
is checking that you have a different file}

      close(datatout);
      assign(datatout, files[_file]);
      settextbuf(datatout, databuf^, 8192);

      if __first_event then
        begin
          rewrite(datatout);
          __first_event:= false;
        end
      else
        append(datatout);
    end;

end; {open}

-- 
Pedt

In Idle village (near Bradford), do they have an Idle Working Mens Club ?
Technical Details
Message-ID: <iIck5MAYzqa2YwMz@pedt.demon.co.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <749jnl$slc$1@nnrp1.dejanews.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111659,df5b94058f04a442
X-Google-Attributes: gid111659,public
X-Google-Thread: 108ebd,df5b94058f04a442
X-Google-Attributes: gid108ebd,public
From: Pedt Scragg <newsmaster@pedt.demon.co.uk>
Subject: Re: Multiple file open/close anomaly?
Date: 1998/12/06
Message-ID: <iIck5MAYzqa2YwMz@pedt.demon.co.uk>
X-Deja-AN: 419338607
X-NNTP-Posting-Host: pedt.demon.co.uk:194.222.191.220
References: <749jnl$slc$1@nnrp1.dejanews.com>
X-Complaints-To: abuse@demon.net
X-Trace: news.demon.co.uk 912963900 nnrp-07:11854 NO-IDENT pedt.demon.co.uk:194.222.191.220
Organization: Ty Llyffant Web
MIME-Version: 1.0
Newsgroups: comp.lang.pascal.borland,borland.public.turbopascal