From: Gautier write-only <gautier_niouzes@hotmail.com>
Subject: Re: Performance of the Streams 'Read and 'Write
Date: Fri, 30 Oct 2009 06:40:27 -0700 (PDT)
Date: 2009-10-30T06:40:27-07:00 [thread overview]
Message-ID: <53b1e21a-1659-462c-bf7f-de8f04b849e1@37g2000yqm.googlegroups.com> (raw)
In-Reply-To: e93ea909-9b8e-44c6-a537-fd68ed47cbc4@z41g2000yqz.googlegroups.com
Here is a test program. I am curious about other results
-- Usage: test_stream_performance <file>
-- Produces two .tmp files that are copies of <file>.
--
-- Example of output with file m.wmv, 2.59 MB, GNAT GPL 2008 / Win32:
--
-- xxx'Write / xxx'Read (Attribute).. 1.925210886 seconds
-- Workaround with SE buffer......... 0.049318559 seconds
-- Factor 39.036235547
-- Buffer size in bits..... 8192
-- SE Buffer size in bits.. 8192
with Ada.Calendar; use Ada.Calendar;
with Ada.Text_IO;
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
with Ada.Command_Line; use Ada.Command_Line;
with Interfaces; use Interfaces;
procedure Test_Stream_Performance is
f_in, f_out: Ada.Streams.Stream_IO.File_Type;
buffer_size, SE_buffer_size: Natural:= 0;
-- To check if buffers could be overlapped (packing)
type Buffer is array(Natural range <>) of Unsigned_8;
------------------------------------------------
-- 1) Stream attributes - xxx'Read, xxx'Write --
------------------------------------------------
-- Usually we would just have: Buffer'Read(Stream(f_in), b);
-- Here we care about end of file.
procedure Read_Attribute( b: out Buffer; last_read: out Natural ) is
idx: constant Positive_Count:= Index(f_in);
siz: constant Positive_Count:= Size(f_in);
begin
if End_Of_File(f_in) then
last_read:= b'First-1;
else
last_read:= Natural'Min(b'First+Natural(siz-idx),b'Last);
Buffer'Read(Stream(f_in), b(b'First .. last_read));
end if;
end Read_Attribute;
procedure Write_Attribute( b: in Buffer ) is
begin
if buffer_size = 0 then
buffer_size:= b'size;
end if;
Buffer'Write(Stream(f_out), b);
end Write_Attribute;
--------------------------------------------
-- 2) The Stream_Element_Array workaround --
--------------------------------------------
procedure Read_SE( b: out Buffer; last_read: out Natural ) is
use Ada.Streams;
First : constant Stream_Element_Offset:= Stream_Element_Offset
(b'First);
Last : Stream_Element_Offset:= Stream_Element_Offset
(b'Last);
SE_Buffer : Stream_Element_Array (First..Last);
begin
Read(Stream(f_in).all, SE_Buffer, Last);
for i in First..Last loop
b(Natural(i)):= Unsigned_8(SE_Buffer(i));
end loop;
last_read:= Natural(last);
end Read_SE;
procedure Write_SE( b: in Buffer ) is
use Ada.Streams;
First : constant Stream_Element_Offset:= Stream_Element_Offset
(b'First);
Last : constant Stream_Element_Offset:= Stream_Element_Offset
(b'Last);
SE_Buffer : Stream_Element_Array (First..Last);
begin
if SE_buffer_size = 0 then
SE_buffer_size:= SE_Buffer'size;
end if;
for i in SE_Buffer'Range loop
SE_Buffer(i):= Stream_Element(b(Natural(i)));
end loop;
Write(Stream(f_out).all, SE_Buffer);
end Write_SE;
name : constant String:= Argument(1);
generic
label: String;
with procedure Read( b: out Buffer; last_read: out Natural );
with procedure Write( b: in Buffer );
procedure Test;
procedure Test is
b: Buffer(1..1024);
l: Natural;
begin
Open(f_in, In_File, name);
Create(f_out, Out_File, name & "_$$$_" & label & ".tmp");
while not End_of_File(f_in) loop
Read(b,l);
Write(b(1..l));
end loop;
Close(f_out);
Close(f_in);
end;
procedure Test_Attribute is new Test("Attribute", Read_Attribute,
Write_Attribute);
procedure Test_SE is new Test("SE", Read_SE, Write_SE);
T0, T1, T2: Time;
use Ada.Text_IO;
begin
T0:= Clock;
Test_Attribute;
T1:= Clock;
Test_SE;
T2:= Clock;
Put_Line("xxx'Write / xxx'Read (Attribute).." & Duration'Image(T1-
T0) & " seconds");
Put_Line("Workaround with SE buffer........." & Duration'Image(T2-
T1) & " seconds");
Put_Line("Factor" & Duration'Image((T1-T0)/(T2-T1)));
New_Line;
Put_Line("Buffer size in bits....." & Integer'Image(buffer_size));
Put_Line("SE Buffer size in bits.." & Integer'Image
(SE_buffer_size));
end;
next prev parent reply other threads:[~2009-10-30 13:40 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-29 23:29 Performance of the Streams 'Read and 'Write Gautier write-only
2009-10-30 0:39 ` Jeffrey R. Carter
2009-10-30 8:58 ` Gautier write-only
2009-10-30 19:12 ` Jeffrey R. Carter
2009-10-31 23:46 ` Gautier write-only
2009-11-01 21:38 ` Gautier write-only
2009-11-02 21:32 ` Randy Brukardt
2009-10-30 3:36 ` Georg Bauhaus
2009-10-30 9:13 ` Gautier write-only
2009-11-02 21:37 ` Randy Brukardt
2009-11-02 22:16 ` Gautier write-only
2009-10-30 13:40 ` Gautier write-only [this message]
2009-11-02 22:19 ` Gautier write-only
2009-11-17 12:57 ` Gautier write-only
2009-11-17 13:26 ` Ludovic Brenta
2009-11-17 20:19 ` Building GNAT on Windows (Was: Re: Performance of the Streams 'Read and 'Write) Tero Koskinen
2009-11-18 10:55 ` Gautier write-only
2009-11-26 13:23 ` Performance of the Streams 'Read and 'Write Gautier write-only
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox