Differences between revisions 1 and 20 (spanning 19 versions)
Revision 1 as of 2008-04-03 13:26:48
Size: 1292
Editor: ZoomQuiet
Comment:
Revision 20 as of 2009-12-25 07:08:58
Size: 6717
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from MicroProj/2008-04-03
Line 4: Line 5:
[[TableOfContents]] <<TableOfContents>>
Line 6: Line 7:
[[Include(ZPyUGnav)]] <<Include(ZPyUGnav)>>

##startInc
Line 16: Line 18:
[http://groups.google.com/group/python-cn/browse_thread/thread/4e4eb6d67a867c21/8b346859a4c96e29#8b346859a4c96e29 v]

== random.pl ==
[[http://groups.google.com/group/python-cn/browse_thread/thread/4e4eb6d67a867c21/8b346859a4c96e29#8b346859a4c96e29|一段python程序的效率问题]]

 从以下对比得出::
  * (a)random.random()函数比random.randrange()函数快。
  * (b)xrange不一定比range快。
  * (c)使用StringIO缓存全部内容,一下子写也不一定快。
  * (d)write() 块写效率会提高。
  * (e)print 到文件句柄 写效率也不错高
  * (f)脚本越短,引用的模块越少效率好

ZoomQuiet 测试环境:
 * HP 520 笔记本电脑 (GQ349AA)
 * 英特尔® 酷睿™ 双核处理器 T2300E 1.66GHz , 2MB 二级高速缓存, 667MHz FSB
 * 内存 2Gb

== time vs 0.1 ==
{{attachment:vs-0.1.png}}

=== 5" random.pl ===
Line 32: Line 50:
== random.py == === 31" random.py ===
Line 53: Line 71:
== time vs ==
attachment:vs-0.1.png


##startInc
    

== time vs 0.2 ==
{{attachment:vs-0.2.png}}

=== 48" random0.2.py ===
{{{#!python
import cStringIO as StringIO

import random
import time

__revision__ = '0.2'

def test():
    fh = open("test_cjj0.2","w")
    output = StringIO.StringIO()
  
    for i in xrange(5000000):

        data = random.randrange(1000000,9999999,1)
        yu = data % 3
        print >>output, yu
   
    fh.write(output.getvalue())

    fh.close()

if __name__ == "__main__" :
    test()
    
}}}

== time vs 0.3 ==
{{attachment:vs-0.3.png}}

=== 33" random0.3.py ===
{{{#!python
import random
import time

__revision__ = '0.3'

def test():
    exp = ""
    for i in xrange(5000000):
        data = random.randrange(1000000,9999999,1)
        exp += "%s\n"%(str(data % 3))
   
    open("test_cjj0.3","w").write(exp)
if __name__ == "__main__" :
    test()
    
}}}


== time vs 0.4 ==
{{attachment:vs-0.4.png}}

=== 27" random0.4.py ===
{{{#!python
import random
__revision__ = '0.4'

fn = open("test_cjj0.4","w")
for i in xrange(5000000):
    data = random.randrange(1000000,9999999,1)
    print >> fn, (data % 3)

}}}


== time vs 0.5 ==
{{attachment:vs-0.5.png}}

=== 51" random0.5.py ===
{{{#!python
import cStringIO as StringIO
import random
__revision__ = '0.5'

#fn = open("test_cjj0.4","w")
out = StringIO.StringIO()
for i in xrange(5000000):
    data = random.randrange(1000000,9999999,1)
    print >> out, (data % 3)

open("test_cjj0.5","w").write(out.getvalue())

}}}

== time vs 0.6 ==
{{attachment:vs-0.6.png}}

=== 26" random0.6.py ===
{{{#!python
import random
__revision__ = '0.6'

fn = open("test_cjj0.6","w")
for i in xrange(5000000):
    print >> fn, (random.randrange(1000000,9999999,1))% 3

}}}
    

== time vs 0.7 ==
{{attachment:vs-0.7.png}}

=== 10" random0.7.py ===
{{{#!python
from random import random
__revision__ = '0.7'

fh = open("test_cjj0.7","w")
for i in range(5000000):
    print >> fh,(int(random()*(9999999-1000000)+1000000))% 3

}}}

== time vs 0.8 ==
{{attachment:vs-0.8.png}}

=== 5" random0.8.py ===
{{{#!python
from random import random

__revision__ = '0.8'

def test():
    fh = open("test_cjj0.8","w")
    for i in range(5000000):
        print >> fh,(int(random()*(9999999-1000000)+1000000))% 3

if __name__ == "__main__" :
    try:
        import psyco
        psyco.full()
    except ImportError:
        pass
    test()
    
}}}

== time vs 0.9 ==
{{attachment:vs-0.9.png}}
 * 使用 pysco 前后的差异

=== 15~11" random0.9.py ===
{{{#!python
from random import random
__revision__ = '0.9'

def test():
   five_million = 5000000
   fh = open("test_cjj0.9","w")
   for i in xrange(five_million):
       data = int(random()*(9999999))
       yu = data % 3
       fh.write('%d\n' % yu)
   fh.close()

if __name__ == "__main__" :
    try:
        import psyco
        psyco.full()
    except ImportError:
        pass
    test()
    
}}}


== time vs 1.0 ==
{{attachment:vs-1.0.png}}
 * 是否打开 pysco 的对比
 * 使用迭代后,进行 pysco 加速反而无益

=== 9~10" random1.0.py ===
{{{#!pythonfrom random import random
__revision__ = '1.0'

def gen_yu():
   for i in xrange(5000000):
       data = int(random()*(9999999))
       yu = data % 3
       yield yu

def test():
   fh = open("test_cjj1.0","w")
   content = '\n'.join([str(m) for m in gen_yu()])
   fh.write(content)
   fh.close()

if __name__ == "__main__" :
    #'''
    try:
        import psyco
        psyco.full()
    except ImportError:
        pass
    #'''
    test()
    
}}}

== time vs end ==
{{{俊杰蔡 <[email protected]>
reply-to [email protected],
to [email protected],
date Sat, Apr 5, 2008 at 2:03 AM
subject [CPyUG:45963] Re: 一段python程序的效率问题
}}}
`Python 脚本胶水威力!`
=== 1" a.py ===

cjj.c:{{{#!cpp
#include <stdio.h>
void amaze()
{
        FILE *fp;
        int i,num;
        fp = fopen("test_cjj","w");
        for(i=0;i<5000000;i++)
        {
                num = (rand()%9000000+1000000) % 3;
                fprintf(fp,"%d\n",num);
        }
        fclose(fp);
}
}}}
cjj.i:{{{
%module cjj
%{
extern void amaze();
%}
extern void amaze();
}}}
编译{{{
swig -python cjj.i \
gcc -c cjj.c cjj_wrap.c -I/usr/include/python2.5 \
ld -shared cjj.o cjj_wrap.o -o _cjj.so
}}}
a.py:{{{#!python
import cjj
__revision__ = '0.1'

if __name__ == "__main__" :
    try:
        import psyco
        psyco.full()
    except ImportError:
        pass
    cjj.amaze()
}}}
运行{{{
$time ./a.py

real 0m1.271s
user 0m1.228s
sys 0m0.036s
}}}
Line 65: Line 338:
创建 by -- ZoomQuiet [[[DateTime(2008-04-03T13:26:48Z)]]]
||<^>[[PageComment2]]||<^>[:/PageCommentData:PageCommentData]''||
创建 by -- ZoomQuiet [<<DateTime(2008-04-03T13:26:48Z)>>]

Py vs Perl 运行

{{{俊杰蔡 <[email protected]> reply-to [email protected], to [email protected], date Thu, Apr 3, 2008 at 5:50 PM subject [CPyUG:45860] }}} 一段python程序的效率问题

从以下对比得出
  • (a)random.random()函数比random.randrange()函数快。
  • (b)xrange不一定比range快。
  • (c)使用StringIO缓存全部内容,一下子写也不一定快。
  • (d)write() 块写效率会提高。
  • (e)print 到文件句柄 写效率也不错高
  • (f)脚本越短,引用的模块越少效率好

ZoomQuiet 测试环境:

  • HP 520 笔记本电脑 (GQ349AA)
  • 英特尔® 酷睿™ 双核处理器 T2300E 1.66GHz , 2MB 二级高速缓存, 667MHz FSB
  • 内存 2Gb

time vs 0.1

vs-0.1.png

5" random.pl

use strict;


open (WW,"> 500000") or die "$!";
foreach(1..5000000){
my $i = int(rand 10000000) % 3;
print WW $i."\n";
}
close WW; 

31" random.py

   1 import random
   2 import time
   3 
   4 __revision__ = '0.1'
   5 
   6 def test():
   7     fh = open("test_cjj","w")
   8    
   9     for i in range(5000000):
  10         data = random.randrange(1000000,9999999,1)
  11         yu = data % 3
  12         fh.write(str(yu)+"\n")
  13                                                   
  14     fh.close()
  15 
  16 if __name__ == "__main__" :
  17     test()

time vs 0.2

vs-0.2.png

48" random0.2.py

   1 import cStringIO as StringIO
   2 
   3 import random
   4 import time
   5 
   6 __revision__ = '0.2'
   7 
   8 def test():
   9     fh = open("test_cjj0.2","w")
  10     output = StringIO.StringIO()
  11   
  12     for i in xrange(5000000):
  13 
  14         data = random.randrange(1000000,9999999,1)
  15         yu = data % 3
  16         print >>output, yu                                          
  17    
  18     fh.write(output.getvalue())
  19 
  20     fh.close()
  21 
  22 if __name__ == "__main__" :
  23     test()

time vs 0.3

vs-0.3.png

33" random0.3.py

   1 import random
   2 import time
   3 
   4 __revision__ = '0.3'
   5 
   6 def test():    
   7     exp = ""
   8     for i in xrange(5000000):
   9         data = random.randrange(1000000,9999999,1)
  10         exp += "%s\n"%(str(data % 3))                             
  11    
  12     open("test_cjj0.3","w").write(exp)
  13 if __name__ == "__main__" :
  14     test()

time vs 0.4

vs-0.4.png

27" random0.4.py

   1 import random
   2 __revision__ = '0.4'
   3 
   4 fn = open("test_cjj0.4","w")
   5 for i in xrange(5000000):
   6     data = random.randrange(1000000,9999999,1)
   7     print >> fn, (data % 3)

time vs 0.5

vs-0.5.png

51" random0.5.py

   1 import cStringIO as StringIO
   2 import random
   3 __revision__ = '0.5'
   4 
   5 #fn = open("test_cjj0.4","w")
   6 out = StringIO.StringIO()
   7 for i in xrange(5000000):
   8     data = random.randrange(1000000,9999999,1)
   9     print >> out, (data % 3)
  10 
  11 open("test_cjj0.5","w").write(out.getvalue())

time vs 0.6

vs-0.6.png

26" random0.6.py

   1 import random
   2 __revision__ = '0.6'
   3 
   4 fn = open("test_cjj0.6","w")
   5 for i in xrange(5000000):
   6     print >> fn, (random.randrange(1000000,9999999,1))% 3

time vs 0.7

vs-0.7.png

10" random0.7.py

   1 from random import random
   2 __revision__ = '0.7'
   3 
   4 fh = open("test_cjj0.7","w")
   5 for i in range(5000000):
   6     print >> fh,(int(random()*(9999999-1000000)+1000000))% 3

time vs 0.8

vs-0.8.png

5" random0.8.py

   1 from random import random
   2 
   3 __revision__ = '0.8'
   4 
   5 def test():
   6     fh = open("test_cjj0.8","w")
   7     for i in range(5000000):
   8         print >> fh,(int(random()*(9999999-1000000)+1000000))% 3
   9 
  10 if __name__ == "__main__" : 
  11     try:
  12         import psyco
  13         psyco.full()
  14     except ImportError:
  15         pass
  16     test()

time vs 0.9

vs-0.9.png

  • 使用 pysco 前后的差异

15~11" random0.9.py

   1 from random import random
   2 __revision__ = '0.9'
   3 
   4 def test():
   5    five_million = 5000000
   6    fh = open("test_cjj0.9","w")
   7    for i in xrange(five_million):
   8        data = int(random()*(9999999))
   9        yu = data % 3
  10        fh.write('%d\n' % yu)
  11    fh.close()
  12 
  13 if __name__ == "__main__" :
  14     try:
  15         import psyco
  16         psyco.full()
  17     except ImportError:
  18         pass
  19     test()

time vs 1.0

vs-1.0.png

  • 是否打开 pysco 的对比
  • 使用迭代后,进行 pysco 加速反而无益

9~10" random1.0.py

__revision__ = '1.0'

def gen_yu():
   for i in xrange(5000000):
       data = int(random()*(9999999))
       yu = data % 3
       yield yu

def test():
   fh = open("test_cjj1.0","w")
   content = '\n'.join([str(m) for m in gen_yu()])
   fh.write(content)
   fh.close()

if __name__ == "__main__" :
    #'''
    try:
        import psyco
        psyco.full()
    except ImportError:
        pass
    #'''
    test()

time vs end

{{{俊杰蔡 <[email protected]> reply-to [email protected], to [email protected], date Sat, Apr 5, 2008 at 2:03 AM subject [CPyUG:45963] Re: 一段python程序的效率问题 }}} Python 脚本胶水威力!

1" a.py

cjj.c:

#include <stdio.h>
void amaze()
{
        FILE *fp;
        int i,num;
        fp = fopen("test_cjj","w");
        for(i=0;i<5000000;i++)
        {
                num = (rand()%9000000+1000000) % 3;
                fprintf(fp,"%d\n",num);   
        }
        fclose(fp);
}

cjj.i:

%module cjj
%{
extern void amaze();
%}
extern void amaze();

编译

swig -python cjj.i \
gcc -c cjj.c cjj_wrap.c -I/usr/include/python2.5 \
ld -shared cjj.o cjj_wrap.o -o _cjj.so

a.py:

   1 import cjj
   2 __revision__ = '0.1'
   3 
   4 if __name__ == "__main__" :
   5     try:
   6         import psyco
   7         psyco.full()
   8     except ImportError:
   9         pass
  10     cjj.amaze()

运行

$time ./a.py

real    0m1.271s
user    0m1.228s
sys     0m0.036s


反馈

创建 by -- ZoomQuiet [2008-04-03 13:26:48]

MiscItems/2008-04-03 (last edited 2009-12-25 07:08:58 by localhost)