TheDcoder Posted August 26, 2016 Share Posted August 26, 2016 Hello! I have thought of this idea just now . I would like to propose an idea today! Its about caching the length of $aArrays or $mMaps! Since repeated use of UBound has its overhead, I would like to propose a method which cache's the length or size of List datatypes (That is what I classify Maps and Arrays under). Here is how it would work: Upon the first measurement of a List type variable, UBound will store the length of the variable along side its pointer in the cache (pointer for future referencing) UBound will check if the pointer already has a cache value associated with it, if yes, it Return the that value instead of recounting. ReDim will update the cache if it exists when resizing an Array. This step might be a little hard for maps, AutoIt would have to update the value of Maps everytime and element is added or removed using MapRemove. To prevent caching of Local variables after the scope has been destroyed, the cached value associated with the pointer will also be destroyed. I might be a fool to think that its not implemented yet... but if has not, I have proposed it just now . Thanks for reading, what do you think of my proposal? EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
Bowmore Posted August 26, 2016 Share Posted August 26, 2016 In what circumstance is it necessary to repeatedly use UBound()? In most of the applications I have written, which are mostly related to heavy data processing, The overhead of UBound is so small, as to be virtually unmeasurable. "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook Link to comment Share on other sites More sharing options...
iamtheky Posted August 26, 2016 Share Posted August 26, 2016 Quote Since repeated use of UBound has its overhead Please elaborate, It doesnt measure the ubound everytime around the loop, otherwise i wouldnt have to count backwards when deleting things. So the penalty would only ever be the initial determination of ubound. Your solution brings about new penalties the beastly one being the logic that flags and persists when a variable has been measured. ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
orbs Posted August 26, 2016 Share Posted August 26, 2016 in general, whenever you think an operation takes too much time, consider yourself at liberty to cache it to a variable in your script. you don't need the AutoIt internals workings to accommodate that. i do that often when it comes to macro or registry reads, and of course when you perform any computation in a loop, when you know for a fact the computation result does not change during the loop. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
JohnOne Posted August 26, 2016 Share Posted August 26, 2016 (edited) My guess is that an autoit array is a vector of its variant, therefore every time an operation occurs on it, the vector properties are updated, there will be no lengthy operation of looping and counting when retrieving Ubound(); Edited August 26, 2016 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jchd Posted August 26, 2016 Share Posted August 26, 2016 UBound is most likely a property of some internal container, hence already "cached" more efficiently than any other wrapper you would invent. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
TheDcoder Posted August 27, 2016 Author Share Posted August 27, 2016 9 hours ago, jchd said: UBound is most likely a property of some internal container, hence already "cached" more efficiently than any other wrapper you would invent. I would be relieved if this were the case . 11 hours ago, Bowmore said: In what circumstance is it necessary to repeatedly use UBound()? Nice question. Like @orbs said, I would cache/store the length, but I am more focussing on a scenario where a UDF would have to use an extra element to store the value in it's return array (The classic example is StringSplit storing the number of elements in [0]). I consider storing the number of elements/items/entries in the 1st element ([0]) as a bad practice, it has some programmatically dis-advantages (I think most of you will agree with me here). So I was thinking of not doing that if UBound cache's the length/size, as it would be easier and the length would not have to be a part of the array itself. 11 hours ago, orbs said: in general, whenever you think an operation takes too much time, consider yourself at liberty to cache it to a variable in your script. you don't need the AutoIt internals workings to accommodate that. Yes, that is what I do when I write a script or a program but as I have said in the above paragraph (in the reply to Bowmore): Quote but I am more focussing on a scenario where a UDF would have to use an extra element to store the value in it's return array Hope you that you have got what I am trying to say here . Anyway, as @JohnOne and @jchd speculated, it would nice if a developer would shed some light on the internal workings of Arrays and UBound . EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
orbs Posted August 27, 2016 Share Posted August 27, 2016 4 hours ago, TheDcoder said: I consider storing the number of elements/items/entries in the 1st element ([0]) as a bad practice, it has some programmatically dis-advantages (I think most of you will agree with me here). a bit hasty conclusion i'd say. i can think of one notorious counter-example: FileReadToArray does not store the count of lines of a text file in element[0], which causes the absurd situation where the array element[1] actually contains text line #2, array element[2] actually contains text line #3, and so on. now tell me that makes sense to you. (luckily, _FileReadToArray has a flag to control that behavior). Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
TheDcoder Posted August 27, 2016 Author Share Posted August 27, 2016 39 minutes ago, orbs said: which causes the absurd situation where the array element[1] actually contains text line #2, array element[2] actually contains text line #3, and so on. It can be easily solved by using a For...In...Next loop, we cannot use the For...In...Next loop for arrays with index in [0]! EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
argumentum Posted August 27, 2016 Share Posted August 27, 2016 Global $n, $aArray[10000000] Global $t = TimerInit() For $n = 1 To UBound($aArray) - 1 ; no matter Next Global $t1 = TimerDiff($t) $t = TimerInit() Global $i = UBound($aArray) - 1 For $n = 1 To $i ; no matter Next Global $t2 = TimerDiff($t) $t = TimerInit() For $n In $aArray ; no matter Next Global $t3 = TimerDiff($t) MsgBox(262144, @ScriptName, "using UBound, time = " & Round($t1, 4) & " mSec." & _ @CR & "using $i, time = " & Round($t2, 4) & " mSec." & _ @CR & "using in, time = " & Round($t3, 4) & " mSec." ) On 8/26/2016 at 4:54 AM, TheDcoder said: I have thought of this idea just now can you show the idea as code ?. I don't get it. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
TheDcoder Posted August 28, 2016 Author Share Posted August 28, 2016 8 hours ago, argumentum said: can you show the idea as code ?. I don't get it. I am afraid that I don't know how to represent the internal mechanisms of Arrays in code... I have clearly made some points in the first posts about how it would work, have you read them? EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
argumentum Posted August 28, 2016 Share Posted August 28, 2016 6 hours ago, TheDcoder said: am afraid that I don't know how to represent the internal mechanisms of Arrays in code... I have clearly made some points So you can not represent it in code of any language but the code should be the way you, dream ? On 8/26/2016 at 4:54 AM, TheDcoder said: UBound will check if the pointer already has a cache value associated with it, if yes, it Return the that value instead of recounting. cache it in your script On 8/27/2016 at 1:28 AM, TheDcoder said: Anyway, as @JohnOne and @jchd speculated, it would nice if a developer would shed some light The only way to shed light is "show me the code" but you'd not know if it's optimal anyway without trial, error and measurement. Unless a developer tells you "yes, it is as you desire, rest easy now" So, the code I posted shows to be optimal as is. You show me code, to show that is otherwise, then I may get your point. ...and my mom told me not to open my mouth unless I'm asked a question, but I still don't seem to understand my mom's wisdom... Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
TheDcoder Posted August 28, 2016 Author Share Posted August 28, 2016 2 minutes ago, argumentum said: So you can not represent it in code of any language but the code should be the way you, dream ? Hey hey, That means I don't have the skill to code "Arrays" ! I think the AutoIt Team does have that skill! 4 minutes ago, argumentum said: cache it in your script On 27/8/2016 at 10:58 AM, TheDcoder said: Like @orbs said, I would cache/store the length, but I am more focussing on a scenario where a UDF would have to use an extra element to store the value in it's return array (The classic example is StringSplit storing the number of elements in [0]). EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now