I want faster result!

I’m trying to get faster result from a pscustomobject. So far it takes about 400 ms to get the data from the following code:

$venfor_list_raw = (Get-Content "D:\mac-vendor.txt" )
$vendor_mac_list = ($venfor_list_raw -replace '\t{1}', ':' | ConvertFrom-Csv -Delimiter ':' -Header MAC,VENDOR)
$VENDOR = (Measure-Command {($vendor_mac_list | Where-Object { $_.MAC -eq '000023' } | Select-Object -ExpandProperty VENDOR )}).TotalMilliseconds
$VENDOR
The object is created from a file with about 25,000 items that look like this:

00001E TELSIST INDUSTRIA ELECTRONICA
00001F Cryptall Communications Corp.
000020 DIAB
000021 SC&C
000022 Visual Technology
000023 ABB Automation AB, Dept. Q
000024 Olicom
000025 RAMTEK CORP.
000026 SHA-KEN CO., LTD.
000027 JAPAN RADIO COMPANY
000028 PRODIGY SYSTEMS CORPORATION
000029 Imc
00002A Trw
00002B CRISP AUTOMATION, INC

Is there a way to make it faster?

 

Generally speaking, running a Get-Content of a file and parsing through it tends to extend time as you are running line by line through a file. There are ways to use your regex value to isolate your array to parse the file without caching the entire file. Personally, I would recommend looking at Select-String as an option.

Just as an example from your code (without knowing what is in your file), I’m going to give it a shot:

$venfor_list_raw = (Get-Content "D:\mac-vendor.txt" )
$vendor_mac_list = ($venfor_list_raw -replace '\t{1}', ':' | ConvertFrom-Csv -Delimiter ':' -Header MAC,VENDOR)
$VENDOR = (Measure-Command {($vendor_mac_list | Where-Object { $_.MAC -eq '000023' } | Select-Object -ExpandProperty VENDOR )}).TotalMilliseconds

Try using something like:

$venfor_list_raw = Select-String -Pattern '000023' -Path "D:\Mac-vendor.txt"
$vendor_mac_list = ($venfor_list_raw.Split('\t{1}')[<number of column for Vendor>]
$VENDOR = (Measure-Command $vendor_mac_list).TotalMilliseconds

<number of column for Vendor> = integer of the column labeled “VENDOR”

In order to make it more dynamic, add something like to the start of the script:

$header = Get-Content "D:\Mac-vendor.txt" -First 1
$count = 0
$columnNumber = $header -split '\t{1}' | Foreach-Object { if ( $_ -ne "VENDOR"){ $count++} else {return $count} }

This will only cache the first line of the file to find which column is assigned “VENDOR”. Then use $columnNumber in place of <number of column for Vendor.

Without knowing what is in the file, it is difficult to test the accuracy of this code.

Thank you for your answer, but while searching a bit more I came up with a hashtable which is 1000x faster!

0.488 ms vs 500 ms !

$HashTable=@{}
foreach($r in $vendor_mac_list)
{
$HashTable[$r.MAC]=$r.VENDEUR
}


(Measure-Command {$VENDEUR = ($HashTable["F074E4"])} ).TotalMilliseconds
$VENDEUR