python win32 替换效率低,为什么python在Windows上这么慢?

I learned about pystones today and so I decided to see what my various environments were like. I ran pystones on my laptop that is running windows on the bare metal and got these results

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> from test import pystone

>>> for i in range(0,10):

... pystone.pystones()

...

(1.636334799754252, 30556.094026423627)

(2.1157907919853756, 23631.82607155689)

(2.5324817108003685, 19743.479207278437)

(2.541626695533182, 19672.4405231788)

(2.536022267835051, 19715.915208695682)

(2.540327088340973, 19682.50475676099)

(2.544761766911506, 19648.20465716261)

(2.540296805235016, 19682.739393664764)

(2.533851636391205, 19732.804905346253)

(2.536483186973612, 19712.3325148696)

Then I ran it on some of our linux VMs and got 2.7-3.4 times better performance. So I fired up my vmware Linux VM on my laptop and reran the same test and got these results:

Python 2.7.2+ (default, Oct 4 2011, 20:03:08)

[GCC 4.6.1] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> for i in range(0,10):

... pystone.pystones()

...

(1.75, 28571.428571428572)

(1.17, 42735.042735042734)

(1.6600000000000001, 30120.48192771084)

(1.8399999999999999, 27173.913043478264)

(1.8200000000000003, 27472.52747252747)

(1.8099999999999987, 27624.30939226521)

(1.3099999999999987, 38167.938931297744)

(1.7800000000000011, 28089.88764044942)

(1.8200000000000038, 27472.527472527414)

(1.490000000000002, 33557.04697986573)

I can't quite understand how the linux VM running inside the same windows is actually FASTER than python running on the same bare metal under windows.

What is so different about python on windows that it performs slower on the bare OS than it does inside a VM running Linux on the same box?

More details

Windows platform Win7x64

32 bit python running on both platforms

32 bit linux VM running the windows platform in VMWare

解决方案

I can't answer your question, however consider this list of things that could be making a difference:

You're using different versions of Python. "2.7.2+" indicates that your linux Python was built from a version control checkout rather than a release.

They were compiled with different compilers (and conceivably meaningfully different optimization levels).

You haven't mentioned reproducing this much. It's conceivable it was a fluke if you haven't.

Your VM might be timing inaccurately.

You're linking different implementations of Python's dependencies, notably libc as Ignacio Vazquez-Abrams points out.

I don't know what pystone's actual benchmarks are like, but many things work differently--things like unicode handling or disk IO could be system-dependent factors.

Pythonwin32com库是Windows平台上自动化Microsoft Office应用程序的一个强大工具,可以用来操作Word、Excel、PowerPoint等Office组件。通过win32com可以实现PPT中文字的替换。以下是一个简单的例子来说明如何使用Pythonwin32com模块来替换PowerPoint演示文稿(PPTX)中的文字。 首先,确保你的环境中安装了win32com库,并且安装了对应的Microsoft PowerPoint应用程序。 ```python import win32com.client as win32 # 启动PowerPoint应用程序 ppt = win32.gencache.EnsureDispatch('Powerpoint.Application') ppt.Visible = True # 可以设置为False让PPT在后台运行 # 打开需要编辑的PPT文件 presentation = ppt.Presentations.Open('你的PPT文件路径.pptx') # 遍历每一张幻灯片中的每一个文本框 for slide in presentation.Slides: for shape in slide.Shapes: if shape.HasTextFrame: # 判断是否包含文本框 for paragraph in shape.TextFrame.Paragraphs: for run in paragraph.Runs: text = run.Text # 这里是替换逻辑,将"要替换的文字"替换为"替换成的文字" new_text = text.replace('要替换的文字', '替换成的文字') run.Text = new_text # 保存并关闭演示文稿 presentation.Save() presentation.Close() # 关闭PowerPoint应用程序 ppt.Quit() ``` 在上面的代码中,我们将演示文稿中的指定文字进行了替换,并保存了更改。需要注意的是,这段代码会替换掉所有匹配的文字,因此在复杂的文档中可能需要更精确的匹配逻辑来避免错误替换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值