Need some help writing a recursive function

btw, using this calling code, you can see that my recursive function is WAYYYYYYY FASTER!!! as much as 2x fast on large column indexes (albeit ms…lol eyeroll)…I am vindicated !!!

clear-host
$stopwatch = [System.Diagnostics.Stopwatch]::new()
$stopwatch.Start()
$txt = myfunction 100000
$stopwatch.Stop()
write-host "Recursive function took", $Stopwatch.Elapsed.Milliseconds, "ms"
write-host $txt

$stopwatch.Start()
Get-ExcelColumnName 100000
$stopwatch.Stop()
write-host "Get-ExcelColumnName took", $Stopwatch.Elapsed.Milliseconds, "ms"

Great. Congrats … You may add the results you’ve got on your machine for those who does not have the ImportExcel module installed. :wink: (if you do, please add them to the same post - do not add a new one. Thanks in advance)

How many Excel sheets with 100,000 or more columns do you usually have to process? :wink: :wink: :crazy_face:

And just BTW: the ImportExcel module is not made by MS. Doug Finke is the developer.

Not to piss on your fireworks but I think your testing is flawed :slight_smile: :fireworks:

Using your code but just reversing the order of execution, I found that if you test Get-ExcelColumnName before myfunction it’s twice as fast as myfunction.

It looks like this has something to do with the stopwatch. If you create a new instance of the stopwatch say:

$stopwatch2 = [System.Diagnostics.Stopwatch]::new()
$stopwatch2.Start()
Get-ExcelColumnName 100000
$stopwatch2.Stop()
write-host "Get-ExcelColumnName took", $Stopwatch2.Elapsed.Milliseconds, "ms"

the timings are mostly identical.

Likewise, with Measure-Command, both your function and Get-ExcelColumnName take a similar amount of time.

PS E:\Temp> (Measure-Command {myfunction 10000}).TotalMilliseconds
2.7121
PS E:\Temp> (Measure-Command {Get-ExcelColumnName 10000}).TotalMilliseconds
2.5198

Lies! all Lies!..Recursion Rules!

well, if it is just as fast, then i will take that as a win.

thanks
:slight_smile: