Is it better to sort in descending order or just sort the list and cancel it?

The module Listcontains a function sortDescendingthat sorts the list from high to low. I read that it’s faster just to sort the list and then cancel it. So I tried, and it looks like what happened.

 [1.0..1000000.0] |> List.sortDescending;;
Real: 00:00:00.322, CPU: 00:00:00.328, GC gen0: 10, gen1: 4, gen2: 0

[1.0..1000000.0] |> List.sort |> List.rev;;
Real: 00:00:00.243, CPU: 00:00:00.250, GC gen0: 15, gen1: 7, gen2: 0

(I tried many times, these values ​​are typical)

Is there any reason to use sortDescendinginstead of sorting the list and then to change the result?

+4
source share
1 answer

, (, ). github (. List.fs local.fs), , , .

sortDescending, stableSortInPlaceWith, stableSortWithKeysAndComparer. , . , .

stableSortInPlace, , float , ; . , List.sort stableSortWithKeysAndComparer, , List.sortDescending, - . , , .

 [1.0..1000000.0] |> List.map Some |> List.sortDescending;;
Real: 00:00:13.616, CPU: 00:00:13.275, GC gen0: 146, gen1: 12, gen2: 5

 [1.0..1000000.0] |> List.map Some |> List.sort |> List.rev;;
Real: 00:00:17.727, CPU: 00:00:17.316, GC gen0: 149, gen1: 15, gen2: 6

, , List.sort ( ,.NET , , ). , , List.sort |> List.rev , List.sortDescending, .

: . , List.sortDescending, List.sort |> List.rev.

+4

Source: https://habr.com/ru/post/1686255/


All Articles