<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Thu Jun 20, 2013 2:21 am by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Thu Jun 20, 2013 2:21 am</pubDate>
  <lastBuildDate>Thu Jun 20, 2013 2:21 am</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>Re: Simple Perl and C comparison</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=100664#100664</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=1444'&gt;gch15&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Jul 22, 2011 1:14 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Thanks for the response. I had guessed some of the issues you mention but not all of them so I have learned. &amp;quot;who knows what Perl interpreter has done?&amp;quot;, however it is good to know that whatever it is doing it is pretty efficient!&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;spaceyhase wrote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;All the memory allocation and copying is killing the C performance.  fgets isn't helping as it is a line-orientated read.  What you should do is figure out the file size (using fseek and ftell, for instance) and allocate once.  We know the length of the file now so the rest is artificial but... Fill the buffer (again, just a single read will do) and count its length (no need to pull in string.h then either).  The best way would be to just keep track of how many bytes have been read as you go - there's no need to count 'em afterwards.  Or, as we know the file size and expect to read the file size, if we do read 'the file size' that should suffice in confirming the length of the 'string'.&lt;br /&gt;
&lt;br /&gt;
And then free the memory.&lt;br /&gt;
&lt;br /&gt;
You can probably do the similar in perl to make it even faster too.&lt;br /&gt;
&lt;br /&gt;
Sorry it's all a bit vague.  It shows the obvious differences between the two languages and that it isn't just a like-for-like comparison (who knows what perl's interpreter has done?; is 'while&amp;lt;in&amp;gt;' functionally the same as 'fgets'?; etc), even though the question itself is a fairly interesting one.&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=100664#100664</comments>
                                        <author>gch15</author>
                                        <pubDate>Fri Jul 22, 2011 1:14 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=100664#100664</guid>
                                      </item>
                                      <item>
                                        <title>Re: Simple Perl and C comparison</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=100371#100371</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=58561'&gt;Bazza&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Thu Jul 07, 2011 3:58 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hi jh...&lt;br /&gt;
&lt;br /&gt;
Would be interesting to know how fast this really is:-&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.linuxformat.com/forums/viewtopic.php?t=11351&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;http://www.linuxformat.com/forums/viewtopic.php?t=11351&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
;o)</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=100371#100371</comments>
                                        <author>Bazza</author>
                                        <pubDate>Thu Jul 07, 2011 3:58 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=100371#100371</guid>
                                      </item>
                                      <item>
                                        <title>Re: Simple Perl and C comparison</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=100358#100358</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=2464'&gt;johnhudson&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Thu Jul 07, 2011 10:14 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      &lt;a href=&quot;http://en.wikipedia.org/wiki/Hello_world_program_examples&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;http://en.wikipedia.org/wiki/Hello_world_program_examples&lt;/a&gt;</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=100358#100358</comments>
                                        <author>johnhudson</author>
                                        <pubDate>Thu Jul 07, 2011 10:14 am</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=100358#100358</guid>
                                      </item>
                                      <item>
                                        <title>Re: Simple Perl and C comparison</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=100356#100356</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=35000'&gt;spaceyhase&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Jul 06, 2011 10:42 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      All the memory allocation and copying is killing the C performance.  fgets isn't helping as it is a line-orientated read.  What you should do is figure out the file size (using fseek and ftell, for instance) and allocate once.  We know the length of the file now so the rest is artificial but... Fill the buffer (again, just a single read will do) and count its length (no need to pull in string.h then either).  The best way would be to just keep track of how many bytes have been read as you go - there's no need to count 'em afterwards.  Or, as we know the file size and expect to read the file size, if we do read 'the file size' that should suffice in confirming the length of the 'string'.&lt;br /&gt;
&lt;br /&gt;
And then free the memory.&lt;br /&gt;
&lt;br /&gt;
You can probably do the similar in perl to make it even faster too.&lt;br /&gt;
&lt;br /&gt;
Sorry it's all a bit vague.  It shows the obvious differences between the two languages and that it isn't just a like-for-like comparison (who knows what perl's interpreter has done?; is 'while&amp;lt;in&amp;gt;' functionally the same as 'fgets'?; etc), even though the question itself is a fairly interesting one.</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=100356#100356</comments>
                                        <author>spaceyhase</author>
                                        <pubDate>Wed Jul 06, 2011 10:42 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=100356#100356</guid>
                                      </item>
                                      <item>
                                        <title>Simple Perl and C comparison</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=100190#100190</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=1444'&gt;gch15&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Jun 29, 2011 10:38 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hi,&lt;br /&gt;
&lt;br /&gt;
I program quite a lot in Perl and find to be fast enough for whatever I want to do. A few days back I thought of comparing Perl with C. Since I often read in lines of text from files I thought I will compare the speed of doing this in Perl and C.&lt;br /&gt;
&lt;br /&gt;
First I generate a text file to read using the BASH code below.&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
&amp;nbsp;if &amp;#91;&amp;#91; -e stuff &amp;#93;&amp;#93;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;then rm stuff;&lt;br /&gt;
&amp;nbsp;fi&lt;br /&gt;
&amp;nbsp;for x in &amp;#123;1..5000&amp;#125;&lt;br /&gt;
&amp;nbsp;do&lt;br /&gt;
&amp;nbsp; echo &amp;quot;This is line $x&amp;quot; &amp;gt;&amp;gt; stuff;&lt;br /&gt;
&amp;nbsp; done&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
If I need a longer test file I just change the 5000 to some bigger number.&lt;br /&gt;
&lt;br /&gt;
Below are a Perl script and a C program. Both do the same thing, which is, read lines from the file (stuff, created above) and keep adding them to a string variable. When all lines have been read, the length of this string variable is printed. That is all. &lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
$ gcc -o for_cmp for_cmp.c&lt;br /&gt;
&lt;br /&gt;
$ time ./for_cmp&lt;br /&gt;
&lt;br /&gt;
88893&lt;br /&gt;
&lt;br /&gt;
real&amp;nbsp; &amp;nbsp;0m1.126s&lt;br /&gt;
user&amp;nbsp; &amp;nbsp;0m1.122s&lt;br /&gt;
sys&amp;nbsp; &amp;nbsp;0m0.003s&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$ time perl for_cmp.pl&lt;br /&gt;
&lt;br /&gt;
88893&lt;br /&gt;
&lt;br /&gt;
real&amp;nbsp; &amp;nbsp;0m0.014s&lt;br /&gt;
user&amp;nbsp; &amp;nbsp;0m0.006s&lt;br /&gt;
sys&amp;nbsp; &amp;nbsp;0m0.007s&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
As you can see above, my C program is significantly slower than the Perl script. My C is &lt;span style=&quot;font-style: italic&quot;&gt;very amateurish&lt;/span&gt; so I believe there must faster ways of doing this in C. I would greatly appreciate an example C code which is faster than (or as fast as) the Perl script in this simple task.&lt;br /&gt;
&lt;br /&gt;
Thanks.&lt;br /&gt;
&lt;br /&gt;
Here is the Perl code&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
# begin perl script for_cmp.pl&lt;br /&gt;
open&amp;#40;IN, &amp;quot;&amp;lt;stuff&amp;quot;&amp;#41;;&lt;br /&gt;
my $growing;&lt;br /&gt;
while&amp;#40;&amp;lt;IN&amp;gt;&amp;#41; &amp;#123;&lt;br /&gt;
$growing .= $_;&lt;br /&gt;
&amp;#125;&lt;br /&gt;
close&amp;#40;IN&amp;#41;;&lt;br /&gt;
print&amp;#40;length&amp;#40;$growing&amp;#41;, &amp;quot;\n&amp;quot;&amp;#41;;&lt;br /&gt;
# end perl script&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
And here is the C code&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
/* begin C code for_cmp.c */&lt;br /&gt;
#define _GNU_SOURCE&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main &amp;#40;int argc, char *argv&amp;#91;&amp;#93;&amp;#41; &amp;#123;&lt;br /&gt;
&amp;nbsp; FILE *infile;&lt;br /&gt;
&amp;nbsp; const size_t mem_chunk = sizeof&amp;#40;char&amp;#41; * 1000 * 500;&lt;br /&gt;
&amp;nbsp; size_t allocd;&lt;br /&gt;
&amp;nbsp; char *growing = &amp;#40;char *&amp;#41; malloc&amp;#40;mem_chunk&amp;#41;;&lt;br /&gt;
&amp;nbsp; char *moving = growing;&lt;br /&gt;
&amp;nbsp; allocd = mem_chunk;&lt;br /&gt;
&amp;nbsp; size_t initsize = 10000;&lt;br /&gt;
&amp;nbsp; char *line = &amp;#40;char *&amp;#41; malloc&amp;#40;sizeof&amp;#40;char&amp;#41; * initsize&amp;#41;;&lt;br /&gt;
&amp;nbsp; char *str = NULL;&lt;br /&gt;
&amp;nbsp; infile = fopen&amp;#40;&amp;quot;stuff&amp;quot;, &amp;quot;r&amp;quot;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; while&amp;#40;&amp;#40;fgets&amp;#40;line, initsize, infile&amp;#41;&amp;#41; != NULL&amp;#41; &amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; if&amp;#40;strlen&amp;#40;growing&amp;#41; + strlen&amp;#40;line&amp;#41; + 100 &amp;gt; allocd&amp;#41; &amp;#123;&lt;br /&gt;
&amp;nbsp; growing = &amp;#40;char *&amp;#41; realloc&amp;#40;growing, allocd + mem_chunk&amp;#41;;&lt;br /&gt;
&amp;nbsp; allocd += mem_chunk;&lt;br /&gt;
&amp;nbsp; moving = growing + strlen&amp;#40;growing&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;#125;&lt;br /&gt;
&amp;nbsp; moving = mempcpy&amp;#40;moving, line, strlen&amp;#40;line&amp;#41;&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;#125;&lt;br /&gt;
&amp;nbsp; printf&amp;#40;&amp;quot;%zu\n&amp;quot;, strlen&amp;#40;growing&amp;#41;&amp;#41;; &lt;br /&gt;
fclose&amp;#40;infile&amp;#41;;&lt;br /&gt;
exit&amp;#40;0&amp;#41;;&lt;br /&gt;
&amp;#125;&lt;br /&gt;
/* end C code */&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=100190#100190</comments>
                                        <author>gch15</author>
                                        <pubDate>Wed Jun 29, 2011 10:38 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=100190#100190</guid>
                                      </item></channel></rss>