Re: [Cython] Feature Request: expand list/tuple in 'if (... in ...):' ...

2013-01-24 Thread Stefan Behnel
[moving this to cython-users]

Christian Inci, 24.01.2013 15:13:
 Subject: Feature Request: expand list/tuple in 'if (... in ...):' ...
 
 expand list/tuple in 'if (... in ...):' on compile-time if the list/tuple 
 contains only static values.
 
 test1: list is expanded manually.
 test2: list is not expanded.
 
 The output of the testcase is:
 test1: timediff: 0.205533
 test2: timediff: 17.567185
 
 
 Testcase:
 
 from time import time
 
 cdef tuple INT_TUPLE = (0x66, 0x67, 0x2e,
  0x36, 0x3e, 0x26, 0x64,
  0x65, 0xf2, 0xf3, 0xf0)
 
 cpdef test():
 cdef unsigned int i
 cdef unsigned short j # 'unsigned char j' won't work for some reason
 cdef double start_time
 start_time = time()
 for i in range(0x5):
 for j in range(256):
 if (j == 0x66 or j == 0x67 or j == 0x2e or j == 0x36 or j == 0x3e 
 or j == 0x26 or j == 0x64 or j == 0x65 or j == 0xf2 or j == 0xf3 or j == 
 0xf0):
 pass
 print(test1: timediff: {0:f}.format(time()-start_time))
 start_time = time()
 for i in range(0x5):
 for j in range(256):
 if (j in INT_TUPLE):
 pass
 print(test2: timediff: {0:f}.format(time()-start_time))

This is not an optimisation as it might render the code incorrect. How can
Cython know that you'll never change INT_TUPLE at runtime?

However, if you use a literal list or tuple in place, this already works,
i.e. the first example is equivalent to

if j in (0x66, 0x67, 0x2e,
 0x36, 0x3e, 0x26, 0x64,
 0x65, 0xf2, 0xf3, 0xf0):

You actually get a C switch statement for it.

If you prefer keeping the set of integer values at a separate place, this
should work:

DEF INT_TUPLE = (0x66, 0x67, 0x2e,
 0x36, 0x3e, 0x26, 0x64,
 0x65, 0xf2, 0xf3, 0xf0)

(mind the upper case DEF).

Stefan
___
cython-devel mailing list
cython-devel@python.org
http://mail.python.org/mailman/listinfo/cython-devel


Re: [Cython] Feature Request: expand list/tuple in 'if (... in ...):' ...

2013-01-24 Thread Christian Inci
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 01/24/2013 03:44 PM, Dag Sverre Seljebotn wrote:
 How do you know that INT_TUPLE isn't reassigned to something else?

On 01/24/2013 03:49 PM, Stefan Behnel wrote:
 This is not an optimisation as it might render the code incorrect. How can
 Cython know that you'll never change INT_TUPLE at runtime?

A good point. What's about something like final or const?

 However, if you use a literal list or tuple in place, this already works,
 i.e. the first example is equivalent to
 
 if j in (0x66, 0x67, 0x2e,
  0x36, 0x3e, 0x26, 0x64,
  0x65, 0xf2, 0xf3, 0xf0):
 
 You actually get a C switch statement for it.

Thanks, this actually works. :-)

 
 If you prefer keeping the set of integer values at a separate place, this
 should work:
 
 DEF INT_TUPLE = (0x66, 0x67, 0x2e,
  0x36, 0x3e, 0x26, 0x64,
  0x65, 0xf2, 0xf3, 0xf0)
 
 (mind the upper case DEF).

But this doesn't work. That's why I'm using 'cdef tuple'.

According to 
'http://docs.cython.org/src/userguide/language_basics.html#compile-time-definitions',
 this will only work using int, long, float or str.
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJRAVDuAAoJEDfL/pqB6n6NsCYP/1HgWbMRuSi4o5o0gIxeSpfl
hkesrLlKe3sodTsWt3cSPGWBg5Y1pq25SUSCLCNssJruXTZ66FCTyMffc3TpEuei
PUSKq+byFplYEmCMPEm1l2z2vBI4+c5JggcXwDLlmp9RR6ZWXft0tb6fg/4Tqwyf
76/l6jrJiI7DImeJwHKDfSs1bHWCIZYPIjQWkeC42kETFG1G+U/L7tfZ8+7oq0pN
fXhRn+XK2ax3iMsswzgIBAuyywASvT2uqMI3/G+MnD7tCiRFZkzILEa3+igxqMYN
H/C9N33XLPZ1UulX40c88LHe1i2fpLvu1IvWoYc42Tszkd8Wd+uASs78b1puFTk+
jeeBLQkGbMle+eCXWQLfnSdmvvn3yYdE1UgVxQ4RXONjol9PSI1r86xn2V97phmW
C/t4r6174hv49wAG1MoMVwXOUJH3ylEBz2qJFq06nwKAU7891dd9O1tXvoVsTihe
NYDk6zflZlPZi7FUMs66S//rYYrXY/ZkA3/NhD9Krbd/Z1cVFTN1J/ynoccMx8dY
pjRz9A+oIzXw/6kvITmGx22iKZRSlhLYe32ZT1qiGk8N1nGAvlaVIqK9mpeQhECX
5CEPSj4+t6T7th7Nh/fMOqJomf0AfzCsuz4QYlYQ8Y+7zfwuOnC8AkkoIid6ac1e
M4vXi93fgnp64RpxKkxS
=Lgq1
-END PGP SIGNATURE-
___
cython-devel mailing list
cython-devel@python.org
http://mail.python.org/mailman/listinfo/cython-devel


Re: [Cython] Feature Request: expand list/tuple in 'if (... in ...):' ...

2013-01-24 Thread Stefan Behnel
Christian Inci, 24.01.2013 16:19:
 On 01/24/2013 03:49 PM, Stefan Behnel wrote:
 If you prefer keeping the set of integer values at a separate place, this
 should work:
 
 DEF INT_TUPLE = (0x66, 0x67, 0x2e,
  0x36, 0x3e, 0x26, 0x64,
  0x65, 0xf2, 0xf3, 0xf0)
 
 (mind the upper case DEF).
 
 According to 
 'http://docs.cython.org/src/userguide/language_basics.html#compile-time-definitions',
  this will only work using int, long, float or str.

Makes sense. It should be doable to make this work by copying AST nodes
around instead of their evaluated Python values. Doesn't sound all that
difficult.

Stefan
___
cython-devel mailing list
cython-devel@python.org
http://mail.python.org/mailman/listinfo/cython-devel