🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Multiple file open/close anomaly?

#419
From: Robert AH Prins
Date: Mon, 07 Dec 1998 00:00
105 lines
4373 bytes
In article <iIck5MAYzqa2YwMz@pedt.demon.co.uk>,
  Pedt Scragg <newsmaster@pedt.demon.co.uk> wrote:
> 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.

Yes, but don't you think (that's at least what I did!) that a simple if test
should be quite a lot faster than closing a file, and opening another one?

> 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.

Correct & correct

> 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}

I only used the conditional to allow easy switching between the two different
modes, the close-and-open-regardless and the
only-close-and-open-when-changed. Conditionals have the advantage that you
don't have to change your source every time - the code is in an include file,
and not a unit.

Robert
--
Robert AH Prins
prinsra@williscorroon.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    
Technical Details
Message-ID: <74g8pv$59r$1@nnrp1.dejanews.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <749jnl$slc$1@nnrp1.dejanews.com> <iIck5MAYzqa2YwMz@pedt.demon.co.uk>

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: Robert AH Prins <prinsra@williscorroon.com>
Subject: Re: Multiple file open/close anomaly?
Date: 1998/12/07
Message-ID: <74g8pv$59r$1@nnrp1.dejanews.com>
X-Deja-AN: 419577950
References: <749jnl$slc$1@nnrp1.dejanews.com> <iIck5MAYzqa2YwMz@pedt.demon.co.uk>
X-Http-Proxy: 1.0 x15.dejanews.com:80 (Squid/1.1.22) for client 193.130.234.1
Organization: Deja News - The Leader in Internet Discussion
X-Article-Creation-Date: Mon Dec 07 09:57:20 1998 GMT
Newsgroups: comp.lang.pascal.borland,borland.public.turbopascal
X-Http-User-Agent: Mozilla/3.0 (Win16; I)