[Cython] Feature Request: expand list/tuple in 'if (... in ...):' ...
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 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)) -BEGIN PGP SIGNATURE- Version: GnuPG v2.0.19 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQIcBAEBAgAGBQJRAUGQAAoJEDfL/pqB6n6NG90P/jWKAw1vjEO1cyem2teyYIce cGWbewGJ14VJycztcnZG6XSNzIC0lLMbeu5edUwmr6ObPD0ic0ncGmuKVcsbUIHe 8JmVOgCxaTl2HnH3BNcvUHmsVeAL5PSyIOch80uOG7Nsh7OjZrQ8fAFqITGHzKy9 wb+C/pO1wF8xj+W9BJ5eYlvUYJLx/t/PZxFExrqrAGUxwlsqDqzoPbQsvvDAW1fB JAMUCR/mLWTQhILOYd1Dvt8gw5uFaiyqqR12f3IOC7yh3xVo3qR3CKFneH4LzuXG 7h7Y8DICXG5xm++kwjBKDbNB+p0o2Z4D56TxdNETxO2vzjnrkIspNhrMpWucvWo3 Wi6qM56W9/2KSupvdpbaOpE0rS1QZxS8v04tpB19c85omCJRDTLlu7Yark7iwo9t M+GZ0sS8CnqD1delky4T/JBT0tWMqGncXX9fWs0FNIjwOtzVY1QnsGmoEl7Sr2wG jPZRrSr2gPoDE9J3+fvGceZF9qEPe9+PDA7boetokxvVS6+mFI3IsPc3wC50uHsz 27gnUH0WovUT7iIcxj/8NACejaaPCR6To9ozfhJbsLEh6YzQc+rQajw/Ag3jr933 LuyEK93PYAVQ2391aPBIDeBcDO0oKvy71foWV3sTTVyu7kKnPLRxtVsXbRv6cfQX 1f5O4GM8SzT1HuNvwMS6 =z5IY -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 ...):' ...
-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