gpt4 book ai didi

performance - 如何在 AppleScript 的处理程序中有效地构建列表?

转载 作者:行者123 更新时间:2023-12-04 20:47:05 25 4
gpt4 key购买 nike

AppleScript 文档建议使用以下代码来有效地构建列表:

set bigList to {}
set bigListRef to a reference to bigList
set numItems to 100000
set t to (time of (current date)) --Start timing operations
repeat with n from 1 to numItems
copy n to the end of bigListRef
end
set total to (time of (current date)) - t --End timing

请注意显式引用的使用。这在脚本的顶层或显式运行处理程序中工作正常,但如果您在另一个处理程序中逐字运行相同的确切代码,如下所示:
on buildList()
set bigList to {}
set bigListRef to a reference to bigList
set numItems to 100000
set t to (time of (current date)) --Start timing operations
repeat with n from 1 to numItems
copy n to the end of bigListRef
end
set total to (time of (current date)) - t --End timing
end buildList
buildList()

它中断,产生一条错误消息,说“无法将 bigList 设为类型引用。”为什么这会中断,在 run() 之外的处理程序中有效构建列表的正确方法是什么?

最佳答案

set end of l to i好像比copy i to end of l快:

on f()
set l to {}
repeat with i from 1 to 100000
set end of l to i
end repeat
l
end f
set t to time of (current date)
set l to f()
(time of (current date)) - t

您还可以使用脚本对象:
on f()
script s
property l : {}
end script
repeat with i from 1 to 100000
copy i to end of l of s
end repeat
l of s
end f
set t to time of (current date)
set l to f()
(time of (current date)) - t

100000也超过 limit of items that can be saved in compiled scripts ,因此如果您运行该脚本并尝试将其保存为 scpt,您将收到这样的错误:

The document “Untitled” could not be saved as “Untitled.scpt”.



您可以输入 set l to f()在处理程序中,因此 l 是本地的,添加 set l to {}到最后,或将脚本另存为 .applescript。

关于performance - 如何在 AppleScript 的处理程序中有效地构建列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15753161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com