[#7872] Nonblocking socket-connect — "Francis Cianfrocca" <garbagecat10@...>

All, I needed a nonblocking socket connect for my asynchronous-event

18 messages 2006/05/14
[#7873] Re: Nonblocking socket-connect — Tanaka Akira <akr@...17n.org> 2006/05/14

In article <3a94cf510605140559l7baa0205le341dac4f47d424b@mail.gmail.com>,

[#7874] Re: Nonblocking socket-connect — "Francis Cianfrocca" <garbagecat10@...> 2006/05/15

How about introducing the method Socket#set_nonblocking, or alternatively

[#7875] Re: Nonblocking socket-connect — Yukihiro Matsumoto <matz@...> 2006/05/15

Hi,

[#7876] Re: Nonblocking socket-connect — "Francis Cianfrocca" <garbagecat10@...> 2006/05/15

Well, it's ok then. I'm comfortable adding in the nonblocking

[#7877] Re: Nonblocking socket-connect — Yukihiro Matsumoto <matz@...> 2006/05/15

Hi,

Re: String#nstrip ?

From: "Berger, Daniel" <Daniel.Berger@...>
Date: 2006-05-18 19:29:31 UTC
List: ruby-core #7905
> -----Original Message-----
> From: Joel VanderWerf [mailto:vjoel@path.berkeley.edu] 
> Sent: Thursday, May 18, 2006 12:41 PM
> To: ruby-core@ruby-lang.org
> Subject: Re: String#nstrip ?
> 
> 
> Daniel Berger wrote:
> > Hi all,
> > 
> > When using the Win32API package, I often have to resort to 
> this idiom 
> > to get a string out of a buffer:
> > 
> > string.split(0.chr).first
> 
> Why won't slice do the trick?
> 
> irb(main):001:0> s = "hello\0\0\0\0"
> => "hello\000\000\000\000"
> irb(main):002:0> s[/^.*?(?=\0)/]
> => "hello"
> 
> -- 
>       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

> -----Original Message-----
> From: ara.t.howard@noaa.gov [mailto:ara.t.howard@noaa.gov] 
> Sent: Thursday, May 18, 2006 1:16 PM
> To: ruby-core@ruby-lang.org
> Subject: Re: String#nstrip ?
> 
> 
> On Fri, 19 May 2006, Daniel Berger wrote:
> 
> > Hi all,
> >
> > When using the Win32API package, I often have to resort to 
> this idiom 
> > to get
> > a string out of a buffer:
> >
> > string.split(0.chr).first
> >
> > The problem is that it's kinda slow.  No, I can't use 
> String#rstrip or
> > String#gsub because often there's other junk at the end of 
> the string that 
> > would prevent those methods from working.
> >
> > What about adding a String#nstrip (null strip) method that would 
> > return a
> > string up to the first NULL character?  Here's a simple 
> implementation:
> 
> harp:~ > ruby -e'  p "42\0\0"[ /^[^\0]*/ ]  '
> "42"
> 
> harp:~ > ruby -e'  p "42"[ /^[^\0]*/ ]  '
> "42"
> 
> harp:~ > ruby -e'  p ""[ /^[^\0]*/ ]  '
> ""

Thanks all.  My benchmarks show the best solutions at about ~4x faster.
I've included a few other solutions folks have provided.

require 'benchmark'

MAX = 100000
STRING = "hello\0\0\0\0m"

Benchmark.bm do |x|
   x.report("String#split"){
      MAX.times{ STRING.split("\0").first }
   }

   # Another attempt by me
   x.report("String#index"){
      MAX.times{ STRING[0 .. STRING.index(0.chr)-1] }
   }

   # Hugh Sasse
   x.report("String#scan"){
      MAX.times{ STRING.scan(/^[^\000]+/).first }
   }

   # Joel VanderWerf
   x.report("String#[regex1]"){
      MAX.times{ STRING[/^.*?(?=\0)/] }
   }

   # Ara Howard
   x.report("String#[regex2]"){
      MAX.times{ STRING[ /^[^\0]*/ ] }
   }
end

String#split  3.174000   0.000000   3.174000 (  3.355000)
String#index  1.733000   0.000000   1.733000 (  2.023000)
String#scan  1.141000   0.000000   1.141000 (  1.212000)
String#[regex1]  0.822000   0.010000   0.832000 (  0.901000)
String#[regex2]  0.691000   0.000000   0.691000 (  0.741000)

Not *quite* as fast as the C version, but good enough I suppose.

Ok, nevermind. :)

Dan


This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly 
prohibited and may be unlawful.  If you have received this communication 
in error, please immediately notify the sender by reply e-mail and destroy 
all copies of the communication and any attachments.


In This Thread

Prev Next