Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8449

Re: C Containers Library vs STL

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: C Containers Library vs STL
Date Thu, 04 Aug 2011 13:38:47 +1200
Lines 69
Message-ID <99ubh7Far7U2@mid.individual.net> (permalink)
References <j1cboc$qj2$1@speranza.aioe.org> <99trvaF9m3U1@mid.individual.net> <tqij37ppjbron0fcdsdvua5e7pigfra7k1@4ax.com> <jejj37prnheru64sshk40btqtan2ll8n5r@4ax.com> <j1clm8$ibc$1@speranza.aioe.org> <99u7p5Far7U1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 8bit
X-Trace individual.net 1e3LTnz4flbJ/9Ft3oPjhwm9lWvRZwAVBsbaugrojD1vOE8rOv
Cancel-Lock sha1:6pTgmaCTX7aOvgjpE3zWIlIITug=
User-Agent Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4
In-Reply-To <99u7p5Far7U1@mid.individual.net>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:8449

Show key headers only | View raw


On 08/ 4/11 12:34 PM, Ian Collins wrote:
> On 08/ 4/11 11:30 AM, jacob navia wrote:
>>
>> Thanks to you and to Ian.
>>
>> More important than the line number, is the behavior of the STL
>> container in case of errors, for instance when there is no more
>> memory.  Does it throw an exception? In that case it would be
>> fully equivalent of the C code that tests for the return code
>> being less than zero.
>>
>> In my Macintosh I compile using gcc the code to
>> -rw-r--r--  1 jacobnavia  staff  6548  3 aoû 23:23 unique.o // CPP
>> -rw-r--r--  1 jacobnavia  staff  1348  3 aoû 23:23 unique1.o // C
>>
>> both are compiled using -Os (size optimization) using the same commpiler
>> ~/Documents/stl $ gcc -v
>> Using built-in specs.
>> Target: i686-apple-darwin10
>> gcc version 4.2.1 (Apple Inc. build 5664)
>>
>>
>> I think that the codes are really equivalent, and I am pleased that
>> the CCL doesn't come all that bad. What performance is concerned
>> I doubt that there would be any differences since the programs are
>> bound by I/O anyway
>
> Um, I tried both versions with the same optimisations (cc/CC -fast) on
> my Solaris box and the run times (over several runs) for a 350K line
> file with 214K unique lines were:
>
> C:  5.027s
> CC: 1.835s

A quick profile shows the C version spends most of its time in strcmp 
while the C++ version spends most of its time in I/O.  So I guess the 
performance difference is mainly between the r-b tree used by std::set 
and your hashing algorithm.

So if I change your code to use std::set in order to remove the 
difference in I/O libraries:

#include <set>
#include <string>
#include <stdio.h>

int main(int argc,char *argv[])
{
   FILE* f = fopen(argv[1],"r");
   if (f == NULL)
     return -1;

   std::set<std::string> unique;
   char buf[8192];
   int i = 0;

   while (fgets(buf,sizeof(buf),f)) {
     if( unique.insert(buf).second ) {
       printf("[%3d] %s",i,buf);
     }
     i++;
   }
   fclose(f);
}

the run time drops to 0.66 seconds.

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-03 22:40 +0200
  Re: C Containers Library vs STL Ian Collins <ian-news@hotmail.com> - 2011-08-04 09:13 +1200
    Re: C Containers Library vs STL Robert Wessel <robertwessel2@yahoo.com> - 2011-08-03 17:36 -0500
      Re: C Containers Library vs STL Robert Wessel <robertwessel2@yahoo.com> - 2011-08-03 17:38 -0500
        Re: C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-04 01:30 +0200
          Re: C Containers Library vs STL Robert Wessel <robertwessel2@yahoo.com> - 2011-08-03 18:40 -0500
            Re: C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-04 13:42 +0200
          Re: C Containers Library vs STL Ian Collins <ian-news@hotmail.com> - 2011-08-04 12:34 +1200
            Re: C Containers Library vs STL Ian Collins <ian-news@hotmail.com> - 2011-08-04 13:38 +1200
              Re: C Containers Library vs STL Juha Nieminen <nospam@thanks.invalid> - 2011-08-04 07:02 +0000
                Re: C Containers Library vs STL Ian Collins <ian-news@hotmail.com> - 2011-08-04 19:40 +1200
                Re: C Containers Library vs STL Juha Nieminen <nospam@thanks.invalid> - 2011-08-04 14:16 +0000
                Re: C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-04 16:25 +0200
                Re: C Containers Library vs STL Nobody <nobody@nowhere.com> - 2011-08-04 21:40 +0100
            Re: C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-04 09:42 +0200
              Re: C Containers Library vs STL Ian Collins <ian-news@hotmail.com> - 2011-08-04 19:59 +1200
              Re: C Containers Library vs STL Marc <marc.glisse@gmail.com> - 2011-08-04 12:47 +0000
                Re: C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-04 15:17 +0200

csiph-web