Showing posts with label $concat. Show all posts
Showing posts with label $concat. Show all posts

Wednesday, 13 June 2012

Undocumented feature - interrupt - part four


Continuing from part three where we looked at "2" and "3"...


4 - Unknown


I haven't found any indication of what functionality this might be, but it does behave differently to "1", which was also unknown.  In this case it always returns a $status of 0 and $result is a blank string.  I tried folders, subfolders, filenames and a blank string, with no difference in behaviour.  I'm giving up, for now.



5 - Working directory


As discussed in part one, using numeric code "5" returns the working directory...

  interrupt(5,"")
  working = $result

The alternative is to use $ldir...

  working = $ldir()

The alternative is more maintainable, safer and also performs better.


6 - Concatenate file path

This takes the file path which is currently in $result and then concatenates the path you pass in, automatically adding the folder delimiters where necessary.  Here's some example code...


  filepath = "C:\temp"
  $result filepath
  interrupt(6,"test")
  filepath = $result



This will then populate $result with "C:\temp\test\".  As you can see, I had excluded the delimiter from the end of both the original $result value and the value I passed it, but the interrupt command fixed that.  Again, this is another string manipulation example, I came up with something like this...



  filepath = "C:\temp" 
  if ( $scan(filepath,"/") < 1 ) ;calculate delimiter
    del = "\"
  else
    del = "/"
  endif
  pos = $length(filepath)
  if ( filepath[pos] != del ) ;check filepath ends with delimiter
    filepath = "%%filepath%%del%%%"
  endif
  filepath = "%%filepath%%%test%%del%%%" ;concatenate filepath



As you can see, this isn't very concise, primarily because I'm accounting for Windows and Unix environments again, meaning that I have to first calculate the "del" local variable.  You could of course use $concat instead of using substitution, like I have.


I tested these two methods over 2,000,000 iterations with the following results...

  • interrupt = 00:06.67, 00:06.75, 00:06.72 (almost 7 seconds)
  • alternative = 00:12.02, 00:12.00, 00:12.04 (about 12 seconds)
  • without $scan = 00:09.04, 00:09.78, 00:09.74 (almost 10 seconds)

I added a third option which was the alternative but with a hardcoded delimiter instead of calculating it, so the performance can be improved by having a global register that holds this.  Still, in this case the interrupt command is better for performance and is less lines of code.  

Summary: Numeric code "4" remains unknown for now, "5" has been replaced by a better alternative and "6" seems to still be useful, although I think I'd still opt for string manipulation which I know that I can control over an undocumented feature.


Wednesday, 16 May 2012

Performance of string manipulation - part two

In part one I looked at the different ways of storing a string during string manipulation, and found that using a variable was by far quicker than using a field.  In this next part, I'm going to look at the different ways of building up the string.  I can think of only two different ways...

  • Concatenation (using $concat)
  • Indirection (eg. "%%string1%%string2%%%")

So I put these to the test with the following four strings...

"ABCDEFGHIJKLMNOPQRSTUVWXYZ "
"01234567890 "
"abcdefghijklmnopqrstuvwxyz "
"01234567890 "

...holding the value in a local variable and iterating 20,000,000 times...

  • Concatenation = 01:03.08, 01:03.71, 01:03.t64 (just over 1 minute)
  • Indirection = 01:02.01, 01:01.74, 01:01.98 (just over 1 minute)

The difference is almost negligible, as I had to go to so many iterations before it could be detected.  Indirection does pip concatenation to the post though, by a whisker.

I then decided to try building up a larger string, by starting with the first string and adding the other three, then taking that string and adding the other three again, and so on.  This time I also used a local variable but iterating only 20,000 times...

  • Concatenation = 00:56.41, 00:56.02, 00:56.37 (just under 1 minute)
  • Indirection = 00:55.61, 00:56.96, 00:56.92 (just under 1 minute)

Again the difference is pretty negligible, proving that in this case, size really doesn't matter.

Summary: Using concatenation or indirection doesn't really make a difference.