Did you know ... Search Documentation:
Pack logtalk -- logtalk-3.86.0/examples/threads/sorting/SCRIPT.txt

This file is part of Logtalk https://logtalk.org/ SPDX-FileCopyrightText: 1998-2023 Paulo Moura <pmoura@logtalk.org> SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

% start by loading the example and the required library files:

| ?- logtalk_load(sorting(loader)). ...

% NOTE: the example queries below use a SWI-Prolog proprietary predicate % time/1 in order to get accurate goal times. This predicate is also found % on recent development versions of YAP and XSB. For other Prolog compilers, % replace the time/1 call by any appropriate timing calls (e.g., cputime/0).

% generate a big list of random floats and then merge sort it using a single thread:

?- generator::list(20000, List), time(msort(1)::msort(List, Sorted)). % 1,145,746 inferences, 0.40 CPU in 0.43 seconds (93% CPU, 2864365 Lips)

List = [0.326219, 0.545052, 0.21687, 0.0500493, 0.772745, 0.805005, 0.574483, 0.301708, 0.670021|...], Sorted = [1.39358e-06, 0.000206126, 0.00026088, 0.000299165, 0.000362691, 0.000397709, 0.000539889, 0.000574419, 0.000578717|...]

Yes

% generate a big list of random floats and then merge sort it using two threads:

?- generator::list(20000, List), time(msort(2)::msort(List, Sorted)). % 80,067 inferences, 0.32 CPU in 0.21 seconds (150% CPU, 250209 Lips)

List = [0.963245, 0.666814, 0.3841, 0.281952, 0.806571, 0.608224, 0.623344, 0.138888, 0.867367|...], Sorted = [5.89827e-05, 0.00010463, 0.000105771, 0.000171936, 0.00022632, 0.000378509, 0.000392918, 0.00041885, 0.000482844|...]

Yes

% generate a big list of random floats and then merge sort it using four threads:

?- generator::list(20000, List), time(msort(4)::msort(List, Sorted)). % 80,079 inferences, 0.32 CPU in 0.16 seconds (204% CPU, 250247 Lips)

List = [0.0923009, 0.443585, 0.72304, 0.945816, 0.501491, 0.311327, 0.597448, 0.915656, 0.666957|...], Sorted = [3.65916e-05, 4.06822e-05, 5.07434e-05, 6.09007e-05, 0.000134275, 0.000190491, 0.00024128, 0.000361441, 0.000412926|...]

Yes

% generate a big list of random floats and then quick sort it using a single thread:

?- generator::list(20000, List), time(qsort(1)::qsort(List, Sorted)). % 1,145,746 inferences, 0.40 CPU in 0.43 seconds (93% CPU, 2864365 Lips)

List = [0.326219, 0.545052, 0.21687, 0.0500493, 0.772745, 0.805005, 0.574483, 0.301708, 0.670021|...], Sorted = [1.39358e-06, 0.000206126, 0.00026088, 0.000299165, 0.000362691, 0.000397709, 0.000539889, 0.000574419, 0.000578717|...]

Yes

% generate a big list of random floats and then quick sort it using two threads:

?- generator::list(20000, List), time(qsort(2)::qsort(List, Sorted)). % 80,067 inferences, 0.32 CPU in 0.21 seconds (150% CPU, 250209 Lips)

List = [0.963245, 0.666814, 0.3841, 0.281952, 0.806571, 0.608224, 0.623344, 0.138888, 0.867367|...], Sorted = [5.89827e-05, 0.00010463, 0.000105771, 0.000171936, 0.00022632, 0.000378509, 0.000392918, 0.00041885, 0.000482844|...]

Yes

% generate a big list of random floats and then quick sort it using four threads:

?- generator::list(20000, List), time(qsort(4)::qsort(List, Sorted)). % 80,079 inferences, 0.32 CPU in 0.16 seconds (204% CPU, 250247 Lips)

List = [0.0923009, 0.443585, 0.72304, 0.945816, 0.501491, 0.311327, 0.597448, 0.915656, 0.666957|...], Sorted = [3.65916e-05, 4.06822e-05, 5.07434e-05, 6.09007e-05, 0.000134275, 0.000190491, 0.00024128, 0.000361441, 0.000412926|...]

Yes