string PowerShell 将整数转换为字符串的速度很慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44767661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PowerShell is slow to convert integers to strings
提问by wecsam
I am writing a PowerShell script where many integers have to be converted to strings. I am using the ToString
method to do this, as in:
我正在编写一个 PowerShell 脚本,其中许多整数必须转换为字符串。我正在使用该ToString
方法来执行此操作,如下所示:
$i = 5
$i.ToString()
Unfortunately, this seems to be very slow (I omitted the execution policy warning):
不幸的是,这似乎很慢(我省略了执行策略警告):
PS I:\ADCC\Scripting Performance> .\int_to_str.ps1
6.747561
PS I:\ADCC\Scripting Performance> .\int_to_str.py
0.37243021680382793
I am using PowerShell 2 and Python 3.
我正在使用 PowerShell 2 和 Python 3。
PS I:\ADCC\Scripting Performance> $PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.5485
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
PS I:\ADCC\Scripting Performance> python --version
Python 3.6.1
This is the contents of int_to_str.ps1
:
这是内容int_to_str.ps1
:
(Measure-Command {
ForEach($i in 1..1000000){
$i.ToString()
}
}).TotalSeconds
This is the contents of int_to_str.py
:
这是内容int_to_str.py
:
#!/usr/bin/env python3
import time
start = time.perf_counter()
for i in range(1, 1000000):
str(i)
print(time.perf_counter() - start)
As you can see, both scripts convert integers from 1 to 1,000,000 to strings. However, while PowerShell takes 6.75 seconds, Python only takes 0.37 seconds, making Python 18 times faster. In the actual PowerShell script that I am writing, it takes about three hours to convert all of the integers to strings, so an 18-fold speed improvement would be welcome.
如您所见,这两个脚本都将 1 到 1,000,000 之间的整数转换为字符串。然而,PowerShell 需要 6.75 秒,而 Python 只需要 0.37 秒,使 Python 快 18 倍。在我正在编写的实际 PowerShell 脚本中,将所有整数转换为字符串大约需要三个小时,因此速度提高 18 倍将是受欢迎的。
Is there a faster way to convert an int
to a string
in PowerShell 2?
在 PowerShell 2 中是否有更快的方法将 an 转换int
为 a string
?
采纳答案by thepip3r
To answer your question, even in .NET (which is what you're using with PowerShell), here is a great article on the int->string conversion you care about: http://cc.davelozinski.com/c-sharp/fastest-way-to-convert-an-int-to-string.
要回答您的问题,即使在 .NET(这是您与 PowerShell 一起使用的)中,这里也有一篇关于您关心的 int->string 转换的精彩文章:http://cc.davelozinski.com/c-sharp /fastest-way-to-convert-an-int-to-string。
As far as byte array conversions from AD, here is a test I did and with explicit casting to a [string[]]
, I nearly always saw gains over using .tostring(). The gains ranged from 50% to equivalent but it was consistently faster:
至于来自 AD 的字节数组转换,这是我所做的一个测试,并且通过显式转换为 a [string[]]
,我几乎总是看到使用 .tostring() 的好处。收益范围从 50% 到同等水平,但始终更快:
$s = [adsisearcher]'(&(objectCategory=user)(samaccountname=mysam))'
$r = @($s.FindAll())
(Measure-Command {
foreach ($b in $r[0].Properties.userpassword[0]) {
$b.tostring()
}
}).TotalSeconds
(Measure-Command {
[string[]]$r[0].Properties.userpassword[0]
}).TotalSeconds
回答by wecsam
I have accepted @thepip3r's answer, but I want to highlight some other possible solutions from the comments on the question two things:
我已经接受了@thepip3r 的回答,但我想从对以下两件事的评论中强调一些其他可能的解决方案:
- You can use
"$i"
instead of$i.ToString()
. It is faster. - If you are on PowerShell 2, you can try downloading a newer version of the Windows Management Framework from Microsoft: https://www.microsoft.com/en-us/download/details.aspx?id=50395
- 您可以使用
"$i"
代替$i.ToString()
. 它更快。 - 如果您使用的是 PowerShell 2,您可以尝试从 Microsoft 下载更新版本的 Windows 管理框架:https: //www.microsoft.com/en-us/download/details.aspx?id=50395
I will edit this if more solutions appear in the comments.
如果评论中出现更多解决方案,我将对其进行编辑。