LINUX.ORG.RU

История изменений

Исправление fsb4000, (текущая версия) :

Судя по всему, у нас немножко отличается логика. В Лабвью у меня каждый из 10 раз происходит генерация 100000 строк, у тебя - только раз, что может дать фору в секунду-две. Надо сместить for _ in 0..10 do повыше.

Исправил.

open System;
open System.Diagnostics;

[<EntryPoint>]
let main argv =
    let random = Random()
    
    let randomStr = 
        let chars = "abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        let charsLen = chars.Length
        
        fun len -> 
            let randomChars = [|for _ in 0..len -> chars.[random.Next(charsLen)]|]
            new string(randomChars)
    
    let randomString100(_) = randomStr(100)
    
    let randomString20(_) = randomStr(20)
    
    for _ in 0..10 do
        let stopWatch = Stopwatch.StartNew()
        let listString100 = List.init 100000 randomString100
        let mutable matches = 0
        for _ in 0..1000 do
            let s20 = randomString20()
            let found = 
                listString100 
                |> List.exists (fun s100 -> s100.Contains(s20))
            if found then matches <- matches + 1
    
        printfn "%d strings matches, time = %f" 
            matches stopWatch.Elapsed.TotalMilliseconds
    0 // return an integer exit code

Repl:

0 strings matches, time = 24167.033200
0 strings matches, time = 23739.963200
0 strings matches, time = 25250.747500
0 strings matches, time = 23176.291200
0 strings matches, time = 23077.360300
0 strings matches, time = 23624.151100
0 strings matches, time = 24353.565100
0 strings matches, time = 23598.490300
0 strings matches, time = 23604.457700
0 strings matches, time = 23379.402600
0 strings matches, time = 23830.667800

А вот памяти стал побольше жрать, увеличивалось понемногу каждую итерацию, видать сборщик мусора решил так надо :): https://imgur.com/a/eb9Nd5d

fsi - это типа F Sharp Interactive

ConsoleApp4 - это типа прога в байткоде

Bytecode:

0 strings matches, time = 15424.552200
0 strings matches, time = 14901.515600
0 strings matches, time = 14619.991000
0 strings matches, time = 13653.333900
0 strings matches, time = 14408.432500
0 strings matches, time = 14945.897600
0 strings matches, time = 14702.474000
0 strings matches, time = 15380.244000
0 strings matches, time = 14522.268400
0 strings matches, time = 13523.723800
0 strings matches, time = 14250.435300

Когда скомпилировал в bytecode, то сборщик мусора работал, максимум памяти было 60 с небольшим мегабайт, но быстро возвращалась к 30 с небольшим…

Исходная версия fsb4000, :

Судя по всему, у нас немножко отличается логика. В Лабвью у меня каждый из 10 раз происходит генерация 100000 строк, у тебя - только раз, что может дать фору в секунду-две. Надо сместить for _ in 0..10 do повыше.

Исправил.

open System;
open System.Diagnostics;

[<EntryPoint>]
let main argv =
    let random = Random()
    
    let randomStr = 
        let chars = "abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        let charsLen = chars.Length
        
        fun len -> 
            let randomChars = [|for _ in 0..len -> chars.[random.Next(charsLen)]|]
            new string(randomChars)
    
    let randomString100(_) = randomStr(100)
    
    let randomString20(_) = randomStr(20)
    
    for _ in 0..10 do
        let stopWatch = Stopwatch.StartNew()
        let listString100 = List.init 100000 randomString100
        let mutable matches = 0
        for _ in 0..1000 do
            let s20 = randomString20()
            let found = 
                listString100 
                |> List.exists (fun s100 -> s100.Contains(s20))
            if found then matches <- matches + 1
    
        printfn "%d strings matches, time = %f" 
            matches stopWatch.Elapsed.TotalMilliseconds
    0 // return an integer exit code

Repl:

0 strings matches, time = 24167.033200
0 strings matches, time = 23739.963200
0 strings matches, time = 25250.747500
0 strings matches, time = 23176.291200
0 strings matches, time = 23077.360300
0 strings matches, time = 23624.151100
0 strings matches, time = 24353.565100
0 strings matches, time = 23598.490300
0 strings matches, time = 23604.457700
0 strings matches, time = 23379.402600
0 strings matches, time = 23830.667800

А вот памяти стал побольше жрать, увеличивалось понемногу каждую итерацию, видать сборщик мусора решил так надо :): https://i.imgur.com/NhSa9fm.png

fsi - это типа F Sharp Interactive

ConsoleApp4 - это типа прога в байткоде

Bytecode:

0 strings matches, time = 15424.552200
0 strings matches, time = 14901.515600
0 strings matches, time = 14619.991000
0 strings matches, time = 13653.333900
0 strings matches, time = 14408.432500
0 strings matches, time = 14945.897600
0 strings matches, time = 14702.474000
0 strings matches, time = 15380.244000
0 strings matches, time = 14522.268400
0 strings matches, time = 13523.723800
0 strings matches, time = 14250.435300

Когда скомпилировал в bytecode, то сборщик мусора работал, максимум памяти было 60 с небольшим мегабайт, но быстро возвращалась к 30 с небольшим…