1D List(1次元配列)の重複項目検出を、複数の方式で速度計測するAppleScriptです。
List(Array)中の重複項目の抽出は、かなりよく出てくる処理です。自分が使っていたルーチンよりも他の人が使っているルーチンのほうが速かったので、ひととおり調べておくべきだと考え、10万項目の乱数データに対して処理してみることに。
テストScriptを書いて実行してみたら、本件では高速化ずみのVanilla ASのハンドラが一番速いという結果が出ました。意外です。
AppleScript名:複数の重複検出ルーチンを順次速度計測.scptd |
— – Created by: Takaaki Naganoya – Created on: 2025/02/05 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions script spd property aList : {} property bList : {} end script –Mesurement data gen. set (aList of spd) to {} repeat with i from 1 to 100000 set the end of (aList of spd) to (random number from 1 to 10000) end repeat –Benchmark Code set hList to {"returnDuplicatesOnly1:", "returnDuplicatesOnly2:", "returnDuplicatesOnly3:"} set resList to {} repeat with i in hList set nameOfTargetHandler to contents of i –Check Handler existence set existsHandler to (me’s respondsToSelector:nameOfTargetHandler) as boolean if existsHandler = true then –Call handler set a1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() set aRes to (my performSelector:nameOfTargetHandler withObject:(aList of spd)) set b1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() set c1Dat to b1Dat – a1Dat else error "Handler does not exists" end if set the end of resList to {nameOfTargetHandler, c1Dat} end repeat return resList –> {{"returnDuplicatesOnly1:", 0.065470099449}, {"returnDuplicatesOnly2:", 2.39611697197}, {"returnDuplicatesOnly3:", 0.038006067276}} –Cocoa Scripting最速? on returnDuplicatesOnly1:aList set arrayOne to current application’s NSArray’s arrayWithArray:aList set setOne to current application’s NSCountedSet’s alloc()’s initWithArray:(arrayOne) set arrayTwo to (arrayOne’s valueForKeyPath:"@distinctUnionOfObjects.self") set setTwo to current application’s NSCountedSet’s alloc()’s initWithArray:(arrayTwo) setOne’s minusSet:setTwo return setOne’s allObjects() as list –>{3, 6, 4} end returnDuplicatesOnly1: –1より遅い on returnDuplicatesOnly2:(aList as list) set aSet to current application’s NSCountedSet’s alloc()’s initWithArray:aList set bList to (aSet’s allObjects()) as list set dupList to {} repeat with i in bList set aRes to (aSet’s countForObject:i) if aRes > 1 then set the end of dupList to (contents of i) end if end repeat return dupList end returnDuplicatesOnly2: –リスト中から重複項目をリストアップする(Vanilla AS高速化版) on returnDuplicatesOnly3:(aList) script spd property aList : {} property dList : {} end script copy aList to (aList of spd) set aCount to length of (aList of spd) set (dList of spd) to {} repeat aCount times set anItem to contents of (first item of (aList of spd)) set (aList of spd) to rest of (aList of spd) if {anItem} is in (aList of spd) then if {anItem} is not in (dList of spd) then –ここを追加した (v3) set the end of (dList of spd) to anItem end if end if end repeat return (dList of spd) end returnDuplicatesOnly3: |