On Wednesday 10 June 2009 12:40:44 AchipA wrote:
> On Jun 10, 7:01 am, Alexey Nezhdanov <[email protected]> wrote:
> > I get these results (best timing):
> >   try-variant: 10.24s
> >   regex: 9.58s
> > So on my laptop try:except: function loses about 5% to regex - probably
> > it depends on hardware/OS.
>
> Interesting, which python/platform are you using ?
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2

Ubuntu 8.04 installed on ASUS A6Q00Km (Turion 64 1.6GHz, SiS motherboard, 1.5G 
RAM).

> > And as Massimo pointed out - we don't really check for an int - we check
> > for valid id. Function should be remamed, yes.
>
> Right, it's more like is_convertible_to_int. Now that we are more
> clear about what we're looking for, it sounds exactly like
> string.isdigit() to me.
yes. I thought that there should be such function but failed to find it. Thank 
you for pointing this out.

> b...@black:/tmp$ python -m timeit "s=[str(i) for i in range(10000)]"
> "import re" "integer_pat=re.compile('[+-]?[0-9]{1,15}$')"
> "is_integer=integer_pat.match"
> 100 loops, best of 3: 3.64 msec per loop
Hmm? No loop...

> b...@black:/tmp$ python -m timeit "s=[str(i) for i in range(10000)]"
> "import re" "integer_pat=re.compile('[+-]?[0-9]{1,15}$')"
> "is_integer=integer_pat.match" "for i in s:"  "  is_integer(i)"
> 100 loops, best of 3: 9.87 msec per loop
roughly the same time here.

> b...@black:/tmp$ python -m timeit "s=[str(i) for i in range(10000)]"
> "for i in s:"  "  i.isdigit()"
> 100 loops, best of 3: 4.63 msec per loop
> Which gives a 6.2x (!) faster result.
I'm sorry, how do you get 6.2x?
9.87/ 4.63 =2.13 here...
And again - you still doing a lot of excess work. First, you populating the 
list, then you iterate over it. You don't need to do either. Look at my 
scripts attached.
Anyways, my testing results are as follows:
I get:
9.84s this time for regex-based 4M calls,
7.70s for function based on 'isdigit'
7.68s for lambda based on 'isdigit'
6.24s for direct 'isdigit' call.
So, Massimo, I think it worths replacing globally each
is_integer(str(x))
to
str(x).isdigit()
If you still prefer having is_integer function then here is it:
is_integer = lambda x: str(x).isdigit()
In this case please rename it to something like is_id.


-- 
Sincerely yours
Alexey Nezhdanov

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Attachment: a.py
Description: application/python

Attachment: b.py
Description: application/python

Attachment: e.py
Description: application/python

Attachment: c.py
Description: application/python

Attachment: sh
Description: application/shellscript

Attachment: d.py
Description: application/python

Reply via email to