Deutsch   English   Français   Italiano  
<vl0sk4$28nvd$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Rich <rich@example.invalid>
Newsgroups: comp.lang.tcl
Subject: Re: good to know: tcl static "regexp" is faster than tcl "string" operation
Date: Tue, 31 Dec 2024 13:46:44 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <vl0sk4$28nvd$1@dont-email.me>
References: <vl0gfm$26irh$1@dont-email.me>
Injection-Date: Tue, 31 Dec 2024 14:46:44 +0100 (CET)
Injection-Info: dont-email.me; posting-host="12c0195d464103dac75cb92dd1eac4a9";
	logging-data="2383853"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19phgpBsAmXEb77DDoHOWXu"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:7mGbMG0a7ZaVPFuLavgMdhdsClQ=
Bytes: 2467

aotto1968 <aotto1968@t-online.de> wrote:
> #!/usr/bin/env tclsh
> 
> proc test-1 { val } {
>   if {[regexp ^:: $val]} {
>     return true
>   } else {
>     return false
>   }
> }
> 
> proc test-2 { val } {
>   if {[string range $val 0 1] eq "::"} {
>     return true
>   } else {
>     return false
>   }
> }
> 
> set num 100000
> puts 1=[time {test-1 ::otto} $num]
> puts 2=[time {test-1 otto}   $num]
> puts 3=[time {test-2 ::otto} $num]
> puts 4=[time {test-2 otto}   $num]
> 
> 
>> ./sbin/time-check.tcl
> 1=1.26311 microseconds per iteration
> 2=1.09152 microseconds per iteration
> 3=1.44028 microseconds per iteration
> 4=1.43917 microseconds per iteration

But neither is quite as fast as string match (athough regex is close):

$ cat time-check.tcl 
#!/usr/bin/env tclsh

proc test-1 { val } {
  if {[regexp ^:: $val]} {
    return true
  } else {
    return false
  }
}

proc test-2 { val } {
  if {[string range $val 0 1] eq "::"} {
    return true
  } else {
    return false
  }
}

proc test-3 { val } {
  return [string match ::* $val]
}

set num 100000
puts 1=[time {test-1 ::otto} $num]
puts 2=[time {test-1 otto}   $num]
puts 3=[time {test-2 ::otto} $num]
puts 4=[time {test-2 otto}   $num]
puts 5=[time {test-3 ::otto} $num]
puts 6=[time {test-3 otto}   $num]

$ ./time-check.tcl 
1=0.45252 microseconds per iteration
2=0.42354 microseconds per iteration
3=0.58949 microseconds per iteration
4=0.58363 microseconds per iteration
5=0.4351 microseconds per iteration
6=0.41378 microseconds per iteration