Robert AH Prin
Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Multiple file open/close anomaly?
In article <J5r+FNAGY7a2Y...@pedt.demon.co.uk>, Pedt Scragg <newsmas...@pedt.demon.co.uk> wrote: Quote> Robert AH Prins [mailto:prin...@williscorroon.com] decided it was an > auspicious moment for: > <opening and closing file if needed> > >> 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? > Should definitely be faster. The file opening/closing overhead is > significant if needed to be called a lot of times in the program. > >{$ifdef fast} > > if _file <> sfile then > > begin; > > sfile:= _file; > >{$endif} > > close(datatout); > > assign(datatout, files[_file]); > The problem with your {$ifdef fast}/{$endif} pairs is that they are > adding the conditional test to the file number but you then run the > close/assign/open part as well.
No, I don't, as can easily be demonstrated by putting a "writeln('Close/Open');" in the code between begin & end. Quote> Leaving the conditionals in but rearranging them and the code slightly > to use the opposite conditional check (I deleted the conditionals from > the previous post which in hindsight perhaps I should not have done) > const > sfile: integer = -1; > procedure open(_file: integer); > begin > {$ifdef fast} > if _file = sfile then exit else > begin > {$endif} > sfile:= _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); > {$ifdef fast} > end; > {$endif} > end; {open}
Your code is doing exactly what my code is doing, be it that we use opposite test to skip the close/open section... But if you look at the code generated for the if-statement in the two cases, you'll see that my code is a trifle shorter: My code: cs:05AE 55 push bp cs:05AF 89E5 mov bp,sp cs:05B1 8B4604 mov ax,[bp+04] cs:05B4 3B060A2A cmp ax,[2A0A] cs:05B8 7503 jne 05BD cs:05BA E98000 jmp 063D cs:05BD 8B4604 mov ax,[bp+04] cs:05C0 A30A2A mov [2A0A],ax Your code: cs:05AE 55 push bp cs:05AF 89E5 mov bp,sp cs:05B1 8B4604 mov ax,[bp+04] cs:05B4 3B060A2A cmp ax,[2A0A] cs:05B8 7506 jne 05C0 cs:05BA E98300 jmp 0640 -+---> Bit silly... cs:05BD E98000 jmp 0640 -+ cs:05C0 8B4604 mov ax,[bp+04] cs:05C3 A30A2A mov [2A0A],ax Quote> > 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. > Yes, and it is a great advantage. I keep source handy for V5, V5.5, V6 > and V7 and find it invaluable not only for version changes but for > switching on and off things like stack and range checks. IMHO, the usage > of conditionals is very underrated.
Robert -- Robert AH Prins prin...@williscorroon.com -----------== Posted via Deja News, The Discussion Network ==---------- http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
|