Bonjour,

Je cherche à optimiser le temps de traitement.
Je suis passé de 2h30 à 1h puis 30 min.
Je me demande s'il y a mieux à faire.

Code Powershell : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
Function ParseData
{
    param
    (
        [string][Parameter(Mandatory=$True)] [string]$path
    )
 
    $results = @() 
    $refresh =  [system.diagnostics.stopwatch]::StartNew()
    $clock   =  [system.diagnostics.stopwatch]::StartNew()  
    $data         = Import-Csv -Path $path -Encoding 'UTF8' -Delimiter ';'
    $data_raw  = $data.NOM | Sort
    $data_uni  = $data_raw     | Sort -Unique
    $index_max = $data_uni.Count ; Write-Host "Nombre d'entrées : $index_max"
 
    $t = Measure-Command {
        $current_index = 0;
        foreach ( $item_1 in $data_uni ) 
        { 
            $occurrences = 0  
            $results_item = @()
 
            $t_loop = Measure-Command {
                foreach ( $item_2 in $data_raw ) 
                { 
                    if ( $item_2 -match $item_1 ) 
                    { 
                        $occurrences++
                        $results_item += $item_2
                    }
                }
            }
 
            $t_end = New-TimeSpan -Seconds $($index_max*$t_loop.TotalSeconds-$Clock.Elapsed.TotalSeconds)
 
            if ( $($refresh.Elapsed.TotalSeconds) -gt 10)
            {
                $refresh.Reset() ; $refresh.Start()
                $progress    = [Math]::Round(100*($current_index)/$index_max,1) ; 
                Write-Host " + Progress : $progress `% - $current_index/$index_max"
                Write-Host " | Temps sous boucle : $($t_loop.TotalSeconds) sec" 
                Write-Host " | Temps restant estimé : $([Int32]$t_end.TotalMinutes) min"
                Write-Host " +----------------------------------------+"
                $refresh++
            }
 
            if ( $occurrences -eq 17 ) { $results += @{ Commune = $item_1 ; Occurence = $occurrences ; Liste = $results_item } }
            $current_index++
        }
 
        $results = $results | % { new-object PSObject -Property $_ } | Select Commune, Occurence, Liste | sort -Descending -Property Occurence
    }
 
    $clock.Stop()
    $refresh.Stop()
    Write-Host "+ Temps d''execution : $($t.TotalMinutes) minutes"
    return $results
}

J'ai notamment testé ceci appliqué à mon cas mais le résultat est pire.

Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
Function test0
{
    $r = @()
    foreach ( $i in 0..99 ) 
    { 
        foreach ( $j in 0..499  ) 
        { 
           $r += $i*$j
        }
    }
    return $r
}
 
Function test1
{
 
    $s = {
            param( $i )
 
            $r = @()
            foreach ( $j in 0..499 ) 
            { 
               $r += $i*$j
            }
            return $r 
    }
 
    $r = @()
    foreach ( $i in 0..99 ) {
            $r += Invoke-Command -ScriptBlock $s -ArgumentList $i
    }
 
    return $r
}
 
$t0 = Measure-Command { $y = test0 }
Write-Host Total : $($t0.TotalSeconds)
$t1 = Measure-Command { $z = test1 }
Write-Host Total : $($t1.TotalSeconds)

Le résultat est sans appel pourtant.

Total : 42,597765
Total : 0,6054444

Merci d'avance.
Cordialement.