PowerShell to create an array the correct, faster method _powershell

Source: Internet
Author: User

Usually when a new object is added to an array, you are most concerned about its performance problems, based on experience. The first example below will tell you the wrong way to do this:

Copy Code code as follows:
Measure-command {
$ar = @ ()

for ($x =0; $x-lt 10000; $x + +)
{
$ar + + $x
}
}

In this loop, the array adds a number of new objects through the symbol "+ +". This will take a long time, because when you change the size of the PowerShell you need to create a new array each time.

Here's a very quick way to-arraylist, which can better handle variable arrays:

Copy Code code as follows:

Measure-command {
$ar = New-object-typename System.Collections.ArrayList

for ($x =0; $x-lt 10000; $x + +)
{
$ar. ADD ($x)
}
}

Two pieces of code for the same thing, the second method will be more efficient.

Experience:

Originally used so long PS found himself has been using the method is not the best, it seems to be the PS play fine, in the final analysis you have to see. NET Master.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.