Slow code


Brian Christmas
 

G’day scripters

Is there any method of speeding up this handler, please? ATM  it’s taking 17 seconds.

Regards

Santa

on checkthecode1()
set (my EnteringCode) to true
say "Please wait" without waiting until completion
set l to ""
set f1 to 19480822
set f2 to 19471206
set m to "239360062088" as text
random number 1.0E+11 to 9.99999999999E+11 with seed 11111111111111 # Not real seed
repeat with x from 1 to 10000
set y to x as text
set f1 to f1 + (random number from 10 to 100)
set f2 to f2 + (random number from 10 to 100)
set m to random number from 1.00000000001E+11 to 9.99999999999E+11 as text
set temp to number_to_string(m)
set l to l & f1 & "-" & f2 & "-" & characters 1 through 6 of temp & "-" & characters 7 through -1 of temp & return as text
end repeat
set (my PassCodes) to l
set (my windowMainFlag) to false
windowMainMM's orderOut:me
windowCode's makeKeyAndOrderFront:me
set focus of Entry1 to true
end checkthecode1

on number_to_string(this_number)
set this_number to this_number as string
if this_number contains "E+" then
set x to the offset of "." in this_number
set y to the offset of "+" in this_number
set z to the offset of "E" in this_number
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
-1 of this_number as string as number
if x is not 0 then
set the first_part to characters 1 thru (x - 1) of this_number as string
else
set the first_part to ""
end if
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
set the converted_number to the first_part
repeat with i from 1 to the decimal_adjust
try
set the converted_number to ¬
the converted_number & character i of the second_part
on error errmsg number errnum
set the converted_number to the converted_number & "0"
end try
end repeat
return the converted_number
else
return this_number
end if
end number_to_string




Brian Christmas
 

G’day Takaaki

I’m trying to generate 10,000 serial numbers, as text, in groups of 8,8,6,6 numbers, with dash separators.

I’ve actually got a script that makes the seed number difficult to determine by reading a text output of the whole script

Random numbers don’t work above 16 digits.

Here’s an early trial, which took 29 seconds.

 
Just thought there might be some voodoo I could use on it, but my existing method is the fastest my experiments could come up with

Reading through your site, and link, I’m uncomfortable using skipjack.

Regards

Santa


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set l to ""
set ll to {}
random number 1.0E+16 to 9.99999999999E+16 with seed 1.111111111E+9
repeat with x from 10000 to 20000
set mm to number_to_string(random number from 1.1111111111111E+13 to 9.9999999999999E+13)
set mmm to number_to_string(random number from 1.1111111111111E+8 to 9.9999999999999E+8)
set m to ((x as text) & mm & mmm)
set end of ll to m
end repeat
repeat with temp in ll
set l to (l & (characters 1 through 8 of temp as text) & "-" & (characters 9 through 16 of temp) & "-" & (characters 17 through 22 of temp) & "-" & (characters 23 through -1 of temp) & return)
end repeat

on number_to_string(this_number)
set this_number to this_number as string
if this_number contains "E+" then
set x to the offset of "." in this_number
set y to the offset of "+" in this_number
set z to the offset of "E" in this_number
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
-1 of this_number as string as number
if x is not 0 then
set the first_part to characters 1 thru (x - 1) of this_number as string
else
set the first_part to ""
end if
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
set the converted_number to the first_part
repeat with i from 1 to the decimal_adjust
try
set the converted_number to ¬
the converted_number & character i of the second_part
on error errmsg number errnum
set the converted_number to the converted_number & "0"
end try
end repeat
return the converted_number
else
return this_number
end if
end number_to_string




On 12 Sep 2017, at 12:38 am, Takaaki Naganoya <maro_ml@...> wrote:

It this a serial-code generator or a simple random number generator?
I don’t know what you need.

We can make simple random number list (item = 10,000) within 0.02 seconds.
http://piyocast.com/as/archives/4353

We can use serial key generator framework
http://piyocast.com/as/archives/3797

--
Takaaki Naganoya
maro@...
http://piyocast.com/as/

2017/09/11 14:42、Brian Christmas <ozsanta@...>のメール:

G’day scripters

Is there any method of speeding up this handler, please? ATM  it’s taking 17 seconds.

Regards

Santa

on checkthecode1()
set (my EnteringCode) to true
say "Please wait" without waiting until completion
set l to ""
set f1 to 19480822
set f2 to 19471206
set m to "239360062088" as text
random number 1.0E+11 to 9.99999999999E+11 with seed 11111111111111 # Not real seed
repeat with x from 1 to 10000
set y to x as text
set f1 to f1 + (random number from 10 to 100)
set f2 to f2 + (random number from 10 to 100)
set m to random number from 1.00000000001E+11 to 9.99999999999E+11 as text
set temp to number_to_string(m)
set l to l & f1 & "-" & f2 & "-" & characters 1 through 6 of temp & "-" & characters 7 through -1 of temp & return as text
end repeat
set (my PassCodes) to l
set (my windowMainFlag) to false
windowMainMM's orderOut:me
windowCode's makeKeyAndOrderFront:me
set focus of Entry1 to true
end checkthecode1

on number_to_string(this_number)
set this_number to this_number as string
if this_number contains "E+" then
set x to the offset of "." in this_number
set y to the offset of "+" in this_number
set z to the offset of "E" in this_number
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
-1 of this_number as string as number
if x is not 0 then
set the first_part to characters 1 thru (x - 1) of this_number as string
else
set the first_part to ""
end if
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
set the converted_number to the first_part
repeat with i from 1 to the decimal_adjust
try
set the converted_number to ¬
the converted_number & character i of the second_part
on error errmsg number errnum
set the converted_number to the converted_number & "0"
end try
end repeat
return the converted_number
else
return this_number
end if
end number_to_string


_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (applescript-users@...)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/applescript-users/maro_ml%40piyocast.com
Archives: http://lists.apple.com/archives/applescript-users

This email sent to maro_ml@...




_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (applescript-users@...)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/applescript-users/ozsanta%40gmail.com
Archives: http://lists.apple.com/archives/applescript-users

This email sent to ozsanta@...


Brian Christmas
 

G’day all

This effort, after pouring over Shanes Ebook, and lots of googling, only stripped 2.4 seconds off the proccessing time, down to 14.3 seconds

Can anyone suggest further or better improvements, please.

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 y to x as text
set f1 to f1 + (random number from 10 to 100)
set f2 to f2 + (random number from 10 to 100)
set m to random number from 1.00000000001E+11 to 9.99999999999E+11 as text
set temp to number_to_string(m)
set l to l & f1 & "-" & f2 & "-" & characters 1 through 6 of temp & "-" & characters 7 through -1 of temp & return as text
end repeat

on number_to_string(this_number)
tell current application's class "NSNumber" to set theNSNumber to this_number
tell current application's class "NSNumberFormatter" to set theResultNSString to localizedStringFromNumber_numberStyle_(theNSNumber, current application's NSNumberFormatterNoStyle)
return theResultNSString as string
end number_to_string

on fixIt()
set c1 to "743196734015346789"
set c2 to "025694701243831634"
set c3 to "690116383350418453"
set x to characters 9 through 14 of c1 & characters 2 through 8 of c2 & characters 11 through 15 of c3 as text as integer
return x
end fixIt


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




Karsten Wolf
 

- Try using a list of strings instead of the string "l". It gets resized 10000 times.


- If it's time critical, try using a different language. AppleScript was not designed for this. A quick Python implementation ran in 0.2s


-karsten


This here clocks at about 5-6s

use AppleScript version "2.4"

use scripting additions


on doit()
set collector 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 r1 to random number from 10 to 100
set r2 to random number from 10 to 100
set f1 to f1 + r1
set f2 to f2 + r2


set r3 to random number from 1000000 to 10000000
set r4 to random number from 1000000 to 10000000
set temp to (r3 as text) & (r4 as text)


set l to (f1 as text) & "-" & f2 & "-" & (characters 1 through 6 of temp) & "-" & (characters 7 through 12 of temp) & return
set end of collector to l
end repeat
set resultstring to collector as string
return resultstring
end doit


on fixIt()
set c1 to "743196734015346789"
set c2 to "025694701243831634"
set c3 to "690116383350418453"
set x to characters 9 through 14 of c1 & characters 2 through 8 of c2 & characters 11 through 15 of c3 as text as integer
return x
end fixIt

doit()



Karsten Wolf
 

Replacing all "as text" with "as string" shaves another 0.5s

-karsten


Brian Christmas
 

G’day Karsten

Thank you very, very much.

I’ve made some further alterations, and in Script Debugger it takes a consistant 3.55 seconds now.

However, in my App, it now takes less than 2 seconds, whereas before the code I had was taking double the time it took in Script Debugger. I was waiting about 30 seconds.

Great thinking, my appreciation!

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()
my doit()
on doit()
set collector 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 r1 to random number from 10 to 100
set r2 to random number from 10 to 100
set f1 to f1 + r1
set f2 to f2 + r2


set r3 to random number from 100000 to 999999
set r4 to random number from 100000 to 999999


set l to (f1 as text) & "-" & f2 & "-" & (r3 as string) & "-" & (r4 as string) & return
set end of collector to l
end repeat
set resultstring to collector as string
return resultstring
end doit


On 12 Sep 2017, at 9:57 pm, Karsten Wolf via Groups.Io <karstenwo@...> wrote:

Replacing all "as text" with "as string" shaves another 0.5s

-karsten






Deivy Petrescu
 

Brian,
not to compete in time, but if you want to generate random serial “numbers” then a much simpler approach would be to use
————
set lista to {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "A", "B", 4, "C", "D", "E", "F"}

set codes to {}
repeat 10000 times
set code to ""
repeat with j from 1 to 28
set code to code & some item of lista
if j mod 4 = 0 and j ≠ 28 then set code to code & "-"
end repeat
set end of codes to code
end repeat
————



Deivy Petrescu
applescript@...


Brian Christmas
 

G’day Nigel

Thank you.

Luckily the seed I REALLY used is 8 digits. I deliberately altered my code for posting.

Very, very useful knowledge, but I HAD extensively tested the overall code to ensure it’s consistency.

Interestingly, the orginal site I got the seed generator/hider handler code from was using a 16 digit number, but NOT for a seed.

Just luck I picked an 8 digit seed! Sometimes, things actually go my way!



I only have one hurdle to overcome now. My trying to get the list of active subscritions from FastSpring is not working. FastSpring's site is utterly awful in not giving examples of what to actually code.

Luckily they have good support, so I’m hoping that overnight my Oz time they’ll tell me how to fix my last problem, then my site can go live, I think. Many minor details fixed in last week.



Quote


Access
Obtain your "API Key", which consists of a username and password. To obtain your API Key, login to the Dashboard and navigate to Integrations > Get API Credentials. The username and password are used for "Basic Authentication" when making requests to the API. A "User-Agent" header is also required by the API to be included in all requests.
Make all requests to https://api.fastspring.com 

unQuote


I’ve tried


fully knowing it would be useless. BUT, I have no idea of what a “User-Agent’ header is...


Regards

Santa




On 13 Sep 2017, at 7:48 pm, Nigel Garvey <gngarvey@...> wrote:

Brian Christmas wrote on Tue, 12 Sep 2017 05:36:56 +1000:

I’m trying to generate 10,000 serial numbers, as text, in groups of
8,8,6,6
numbers, with dash separators.

Brian Christmas wrote on Wed, 13 Sep 2017 10:25:30 +1000

However, I want a reproducable list of codes.

On my machine, the codes are only reproduceable if the random number
seed integer is restricted to nine digits or fewer.

 use AppleScript version "2.4" -- Yosemite (10.10) or later
 use framework "Foundation"
 use scripting additions

 set theSeed to fixit()
 random number with seed theSeed

 -- Generate serial numbers and gather them into a set to ensure no dupes. Stop at 10,000 unique entries.
 set serialNumbers to current application's class "NSMutableSet"'s new()
 set astid to AppleScript's text item delimiters
 set AppleScript's text item delimiters to "-"
 repeat until (serialNumbers's |count|() is 10000)
   tell serialNumbers to addObject:({(random number 89999999) + 10000000, (random number 89999999) + 10000000, (random number 899999) + 100000, (random number 899999) + 100000} as text)
 end repeat
 set AppleScript's text item delimiters to astid
 set serialNumbers to serialNumbers's allObjects()

 -- Either:
 -- set serialNumbers to serialNumbers as list
 -- Or:
 set serialNumbers to (serialNumbers's componentsJoinedByString:(linefeed)) as text

 on fixit()
   set c1 to "743196734015346789"
   set c2 to "025694701243831634"
   set c3 to "690116383350418453"
   set x to text 9 through 11 of c1 & text 2 through 5 of c2 & text 11 through 12 of c3 as integer
   return x
 end fixit


NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (applescript-users@...)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/applescript-users/ozsanta%40gmail.com
Archives: http://lists.apple.com/archives/applescript-users

This email sent to ozsanta@...