Skip to content

Commit

Permalink
v1.2
Browse files Browse the repository at this point in the history
+ Add improved examples
  • Loading branch information
milesgratz authored Apr 20, 2017
1 parent 98a3faf commit 563030f
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 205 deletions.
Binary file modified FastLookup.psd1
Binary file not shown.
85 changes: 62 additions & 23 deletions FastLookup.psm1
Original file line number Diff line number Diff line change
@@ -1,40 +1,79 @@
#requires -version 3
<#
.SYNOPSIS
Lookup a value in an array faster than Where-Object
Designed to optimize the speed of searching an array.
.DESCRIPTION
Improve the speed of looking up a value in an array by creating a hashtable index.
Good for looking up results in very large arrays or CSV files (e.g Import-Csv)
Improve the speed of looking up a value in an array by creating a hashtable index.
Good for looping through very large arrays or CSV files
.NOTES
Version: 1.1
Author: Miles Gratz
Creation Date: April 11, 2017
Purpose/Change: Initial script development
Version: 1.2
Author: Miles Gratz
Creation Date: April 20, 2017
Purpose/Change: Improve examples
.EXAMPLE
$array = 1..10000000
$hashtable = New-FastLookup $array
Measure-Command { $array | Where-Object { $_ -eq 199999 } }
PS>
$array = 1..1000000
$hashtable = New-FastLookup -Array $array
Get-FastLookup -Value 2017 -Array $array -Table $hashtable
Days : 0
Hours : 0
Minutes : 0
Seconds : 9
Milliseconds : 714
.EXAMPLE
PS>
# Search for thousand random numbers in an array of one million
$array = 1..1000000
$search = 1..1000 | ForEach-Object {
Get-Random -Maximum $array.Count
}
---------------------------------------------------------------
Where-Object Performance Test
Measure-Command {
$array | Where-Object { $_ -in $search }
}
Minutes : 2
Seconds : 39
Milliseconds : 658
---------------------------------------------------------------
ForEach Performance Test
Measure-Command {
foreach ($item in $array){ if ($item -in $search){ $item } }
}
Minutes : 1
Seconds : 27
Milliseconds : 460
---------------------------------------------------------------
FastLookup Performance Test
Measure-Command {
$hashtable = New-FastLookup -Array $array
foreach ($item in $search){
Get-FastLookup -Value $item -Array $array -Table $hashtable
}
}
Measure-Command { Get-FastLookup -Value 199999 -Array $array -Table $hashtable }
Minutes : 0
Seconds : 49
Milliseconds : 933
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 65
---------------------------------------------------------------
[NOTE] Performance test on Windows 10 x64 (i5-6200U, 8GB RAM, SSD)
[NOTE] Performance test on Windows 10 x64 (i5-6200U/8GB/SSD)
#>

$Functions = @( Get-ChildItem -Path $PSScriptRoot\*.ps1 -ErrorAction SilentlyContinue )
Expand Down
93 changes: 64 additions & 29 deletions Get-FastLookup.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
function Get-FastLookup {
<#
.SYNOPSIS
Lookup a value in an array faster than Where-Object
Designed to optimize the speed of searching an array.
.DESCRIPTION
Improve the speed of looking up a value in an array by creating a hashtable index.
Good for looking up results in very large arrays or CSV files (e.g Import-Csv)
Good for looping through very large arrays or CSV files
.NOTES
Version: 1.1
Version: 1.2
Author: Miles Gratz
Creation Date: April 11, 2017
Purpose/Change: Initial script development
Creation Date: April 20, 2017
Purpose/Change: Improve examples
.PARAMETER Array
A mandatory parameter specifying the array used to create a 'FastLookup' (hashtable index)
Expand All @@ -26,33 +26,68 @@
The object(s) in the array that match the search
.EXAMPLE
PS>
$array = 1..1000000
$hashtable = New-FastLookup -Array $array
Get-FastLookup -Value 2017 -Array $array -Table $hashtable
.EXAMPLE
PS>
# Search for thousand random numbers in an array of one million
$array = 1..1000000
$search = 1..1000 | ForEach-Object {
Get-Random -Maximum $array.Count
}
---------------------------------------------------------------
PS> $array = 1..10000000
PS> $hashtable = New-FastLookup $array
PS> Measure-Command {
$array | Where-Object { $_ -eq 199999 }
}
Days : 0
Hours : 0
Minutes : 0
Seconds : 9
Milliseconds : 306
PS> Measure-Command {
Get-FastLookup -Value 199999 -Array $array -Table $hashtable
}
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 65
[NOTE] Performance test on Windows 10 x64 (i5-6200U, 8GB RAM, SSD)
Where-Object Performance Test
Measure-Command {
$array | Where-Object { $_ -in $search }
}
Minutes : 2
Seconds : 39
Milliseconds : 658
---------------------------------------------------------------
ForEach Performance Test
Measure-Command {
foreach ($item in $array){ if ($item -in $search){ $item } }
}
Minutes : 1
Seconds : 27
Milliseconds : 460
---------------------------------------------------------------
FastLookup Performance Test
Measure-Command {
$hashtable = New-FastLookup -Array $array
foreach ($item in $search){
Get-FastLookup -Value $item -Array $array -Table $hashtable
}
}
Minutes : 0
Seconds : 49
Milliseconds : 933
---------------------------------------------------------------
[NOTE] Performance test on Windows 10 x64 (i5-6200U/8GB/SSD)
#>

param(
[Parameter(Mandatory=$true)]
$Value,
Expand Down
85 changes: 60 additions & 25 deletions New-FastLookup.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
function New-FastLookup {
<#
.SYNOPSIS
Lookup a value in an array faster than Where-Object
Designed to optimize the speed of searching an array.
.DESCRIPTION
Improve the speed of looking up a value in an array by creating a hashtable index.
Good for looking up results in very large arrays or CSV files (e.g Import-Csv)
Good for looping through very large arrays or CSV files
.NOTES
Version: 1.1
Version: 1.2
Author: Miles Gratz
Creation Date: April 11, 2017
Purpose/Change: Initial script development
Creation Date: April 20, 2017
Purpose/Change: Improve examples
.PARAMETER Array
A mandatory parameter specifying input array used to create 'FastLookup'
Expand All @@ -23,33 +23,68 @@
A hashtable, listing the values in the array and their corresponding index
.EXAMPLE
PS>
$array = 1..1000000
$hashtable = New-FastLookup -Array $array
Get-FastLookup -Value 2017 -Array $array -Table $hashtable
.EXAMPLE
PS>
# Search for thousand random numbers in an array of one million
$array = 1..1000000
$search = 1..1000 | ForEach-Object {
Get-Random -Maximum $array.Count
}
---------------------------------------------------------------
PS> $array = 1..10000000
PS> $hashtable = New-FastLookup $array
Where-Object Performance Test
Measure-Command {
$array | Where-Object { $_ -in $search }
}
Minutes : 2
Seconds : 39
Milliseconds : 658
PS> Measure-Command {
$array | Where-Object { $_ -eq 199999 }
}
---------------------------------------------------------------
Days : 0
Hours : 0
Minutes : 0
Seconds : 9
Milliseconds : 306
ForEach Performance Test
PS> Measure-Command {
Get-FastLookup -Value 199999 -Array $array -Table $hashtable
}
Measure-Command {
foreach ($item in $array){ if ($item -in $search){ $item } }
}
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 65
Minutes : 1
Seconds : 27
Milliseconds : 460
[NOTE] Performance test on Windows 10 x64 (i5-6200U, 8GB RAM, SSD)
---------------------------------------------------------------
FastLookup Performance Test
Measure-Command {
$hashtable = New-FastLookup -Array $array
foreach ($item in $search){
Get-FastLookup -Value $item -Array $array -Table $hashtable
}
}
Minutes : 0
Seconds : 49
Milliseconds : 933
---------------------------------------------------------------
[NOTE] Performance test on Windows 10 x64 (i5-6200U/8GB/SSD)
#>

param(
[Parameter(Mandatory=$true)]
[array]$Array,
Expand Down Expand Up @@ -118,4 +153,4 @@

# Output results
$HashTable
}
}
Loading

0 comments on commit 563030f

Please sign in to comment.