Re: Slow code


Brian Christmas
 

Of interest, it appears the random number generation is the bottleneck.

I stripped and simplified the code to the first example below, and it averages 11.5 seconds, a drop of about 5 seconds. Using brackets ie (characters 1 through 6 of temp)  sped things up, (and adding a ‘my’ in front of the number formatter caller of my old code added 2 seconds.)

I then modified the code as per the second example, and the time average did not alter.

Any way to speed up the generation of a random number, please?

It’s also remarkable how many seconds each time can vary by! + - 2 seconds either side of a rough average.

Regards

Santa


use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions
set l to ""
set f1 to 19480822
set f2 to 19471206
random number 1.0E+11 to 9.99999999999E+11 with seed my fixIt()
repeat with x from 1 to 10000
set f1 to f1 + (random number from 10 to 100) # NEED THESE as they’re always different, whereas the last one MIGHT be a duplicate occasionally
set f2 to f2 + (random number from 10 to 100)
tell current application's class "NSNumber" to set theNSNumber to (random number from 1.00000000001E+11 to 9.99999999999E+11)
tell current application's class "NSNumberFormatter" to set temp to (localizedStringFromNumber_numberStyle_(theNSNumber, current application's NSNumberFormatterNoStyle)) as string
set l to (l & f1 as text) & "-" & f2 & "-" & (characters 1 through 6 of temp) & "-" & (characters 7 through 12 of temp) & return
end repeat

 

 on fixIt()
# Allready posted altered version
return x
end fixIt



use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions
set l to ""
set f1 to 19480822
set f2 to 19471206
random number 1.0E+11 to 9.99999999999E+11 with seed my fixIt()
repeat with x from 1 to 10000
set f1 to f1 + (random number from 10 to 100)
set f2 to f2 + (random number from 10 to 100)
set temp to (random number from 100001 to 999999)
set temp2 to (random number from 100001 to 999999)
set l to (l & f1 as text) & "-" & f2 & "-" & temp & "-" & temp2 & return
end repeat

on fixIt()
# Allready posted altered version
return x
end fixIt



Join applescript@apple-dev.groups.io to automatically receive all group messages.