[#1649] Re: New Ruby projects — Yukihiro Matsumoto <matz@...>
The following message is a courtesy copy of an article
[#1672] Re: Ruby 1.4 stable manual bug? — Yukihiro Matsumoto <matz@...>
The following message is a courtesy copy of an article
[#1673] Re: Possible problem with ext/socket in 1.5.2 — itojun@...
[#1694] Conventions for our Ruby book — Dave Thomas <Dave@...>
[#1715] Install postgresql support — Ikhlasul Amal <amal@...>
Hi all,
Hi,
[#1786] Is this a bug? — Clemens Hintze <clemens.hintze@...>
(mailed & posted)
[#1814] Objects nested sometimes. — Hugh Sasse Staff Elec Eng <hgs@...>
I am attemptiong to write a package which consists of a workspace
[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>
Hi all,
Hi,
Yukihiro Matsumoto writes:
Hi,
Hi,
[#1834] enum examples? — Hugh Sasse Staff Elec Eng <hgs@...>
Has anyone any examplse of using the Enumerable module? I've had a
[#1844] Minor irritation, can't figure out how to patch it though! — Hugh Sasse Staff Elec Eng <hgs@...>
I was considering how difficult it would be to patch Ruby to accept
[#1889] [ruby-1.5.3] require / SAFE — ts <decoux@...>
[#1896] Ruby Syntax similar to other languages? — "David Douthitt" <DDouthitt@...>
From: Yukihiro Matsumoto <matz@netlab.co.jp>
[#1900] Enumerations and all that. — Hugh Sasse Staff Elec Eng <hgs@...>
Thank you to the people who responded to my questions about Enumerated
Hugh Sasse Staff Elec Eng <hgs@dmu.ac.uk> writes:
On 16 Mar 2000, Dave Thomas wrote:
[#1929] Re: Class Variables — "David Douthitt" <DDouthitt@...>
| "David Douthitt" <DDouthitt@cuna.com> writes:
[#1942] no Fixnum#new ? — Quinn Dunkan <quinn@...>
Ok, I can add methods to a built-in class well enough (yes I know about succ,
[#1989] English Ruby/Gtk Tutorial? — schneik@...
Hi,
[#2022] rb_global_entry — ts <decoux@...>
[#2036] Anonymous and Singleton Classes — B_DAVISON <Bob.Davison@...>
I am a Ruby newbie and having some problems getting my mind around certain
[#2069] Ruby/GTK+ question about imlib --> gdk-pixbug — schneik@...
[#2073] Re: eval.rb fails — "Dat Nguyen" <thucdat@...>
The doc is fine, this happens only if you try to execute 'until' block
On Wed, 22 Mar 2000, Dat Nguyen wrote:
[#2084] Scope violated by import via 'require'? — Clemens Hintze <c.hintze@...>
Hi,
[#2104] ARGF or $< — Hugh Sasse Staff Elec Eng <hgs@...>
Has anyone any examples of how to use ARGF or $< as I cannot find much
Hi.
[#2165] Ruby strict mode and stand-alone executables. — "Conrad Schneiker" <schneiker@...>
Some people want Ruby to have a strict compile mode.
[#2203] Re: parse bug in 1.5 — schneik@...
[#2212] Re: Ruby/Glade usage questions. — ts <decoux@...>
>>>>> "m" == mrilu <mrilu@ale.cx> writes:
[#2241] setter() for local variables — ts <decoux@...>
[#2256] Multiple assignment of pattern match results. — schneik@...
[#2267] Re: Ruby and Eiffel — h.fulton@...
[#2309] Question about attribute writers — Dave Thomas <Dave@...>
Clemens Hintze <c.hintze@gmx.net> writes:
[ruby-talk:02206] Re: Scripting and OO -- thought question
David Douthitt writes:
...
> Well, the Ruby version used to do:
>
> LogFile.open.readlines.each { }
>
> Now it does
>
> LogFile.open { |f| f.readlines.each { } }
>
...
> This following ruby code was replaced entirely by (ksh):
>
> for sys in $*
> do
> egrep ":.. [^ ]* (in\.|)$sys" $MESSAGES > ${sys}
> done
>
> (MESSAGES=/var/log/messages)
>
> And it FLIES! Here is the ruby code:
^^^^^
Arrrggghhhhh!!!
>
> #!/usr/bin/ruby
...
> systems = Array.new
>
> class String
> def systemName
> self.scan("^... .. ..:..:.. ([^ ]*)")
> end
> end
>
> File.open("/var/log/messages").each { |line|
> line.chomp!
> # sys = line.scan("^... .. ..:..:.. ([^ ]*)")
> sys = line.systemName
>
> systems = systems | sys
>
> if not ARGV.include?(sys[0][0])
> print(" ", line, "\n")
> end
> }
Of course you have guessed it! Somebody has to complain now. This
comparsion is totally unfair. And I assume you know this fact!!! :-(
In your example 'grep' will do nearly all of the true work. The ksh
will serve only as wrapper and call 'grep' once for every system you
have passed via command line.
The ruby program, however, will do real work. No external program
is helping. Of course, most often a C program will be faster than a
plain Ruby one. But this will not save you ;-)))
Do you remember? In the original thread I had said: "I strongly
believe that my app using scripting and C together would be nearly as
performant as his one coded in C/C++ only." :-)))))))))
Okay, here we go:
Following is your example. I had to change the regexp a little bit,
otherwise it wouldn't match my syslog entries:
Okay. Now the files necessary for Ruby. First the extconf.rb file that
configure our extension;
Then our Ruby program that do nearly the same as your ksh script
(means nothing ;-):
Then the extension module, the C code. This file serves only as prove
of concept and is not bullet proof coded (in fact no any error
handling):
So now I have done the following:
------------------------------------------------
Script started on Tue Mar 28 04:12:24 2000
cle@qiao > ruby extconf.rb
creating Makefile
cle@qiao > make
gcc -fPIC -I/usr/local/lib/ruby/1.5/i586-linux -g -O2 -fPIC -I/usr/local/include -c _sysgrep.c -o _sysgrep.o
gcc -shared -o _sysgrep.so _sysgrep.o -lc
cle@qiao > time sysgrep.ksh qiao qiao qiao qiao # your example
real 0m4.859s
user 0m1.990s
sys 0m1.250s
cle@qiao > time sysgrep.rb qiao qiao qiao qiao # my one
real 0m4.169s
user 0m2.070s
sys 0m1.080s
cle@qiao > exit
Script done on Tue Mar 28 04:13:13 2000
------------------------------------------------
As you can see, now Ruby is even faster than your ksh solution. Of
course this evidence is not fair too, but in confirmation with my
cited statement above! :-)
And it is not more unfair than yours ;-))))
[nice code deleted ...]
If you would try to do this all in ksh *without* help from grep and in
the same complexity as your fine Ruby script, I guess you will go mad!
And I have my doubts, if this *pure* ksh solution would be faster than
the Ruby's one ;-)
> Now aren't you glad you asked for code? :-)
I am for sure. I love to see code written by others. I can learn from
anyone! :-)
\cle
--
Clemens Hintze mailto: c.hintze@gmx.net
Attachments (4)
require "mkmf" create_makefile "_sysgrep"
#include <ruby.h>
#include <strings.h>
#define MAXLIN 3072
static VALUE grep(VALUE self, VALUE offset, VALUE host, VALUE infile)
{
FILE* fpin = fopen(RSTRING(infile)->ptr, "r");
FILE* fpout = fopen(RSTRING(host)->ptr,"w");
char line[MAXLIN];
char* sys = RSTRING(host)->ptr;
long syslen = RSTRING(host)->len;
char* syspos = line + NUM2LONG(offset);
while (fgets(line, MAXLIN, fpin))
if (strncmp(syspos, sys, syslen) == 0)
fprintf(fpout, line);
fclose(fpin);
fclose(fpout);
return Qnil;
}
void Init__sysgrep()
{
rb_define_global_function("grep", grep, 3);
}