Zip cycle list, which way is efficient?
As an example, given a list xs = [1..10]
, the thing I want is:
[(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9),(9,10),(10,1)]
my solution is
zip xs (tail xs ++ [head xs]) -- solution (1)
and someone suggests that
zip xs (tail . cycle $ xs) -- solution (2)
but I don't know whether the solution (2) is more efficient? or two solutions are equivalent?
haskell
add a comment |
As an example, given a list xs = [1..10]
, the thing I want is:
[(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9),(9,10),(10,1)]
my solution is
zip xs (tail xs ++ [head xs]) -- solution (1)
and someone suggests that
zip xs (tail . cycle $ xs) -- solution (2)
but I don't know whether the solution (2) is more efficient? or two solutions are equivalent?
haskell
Have you tried timing the two methods and seeing for yourself?
– AJFarmar
Dec 29 '18 at 13:15
@AJFarmar I have not learned how to timing the Haskell program.
– JoeChoi
Dec 29 '18 at 13:18
2
I would expect these solutions to be similar in performance. One could try thecriterion
library to benchmark them. serpentine.com/criterion
– chi
Dec 29 '18 at 13:23
@chi Thank you very much. I shall try it later.
– JoeChoi
Dec 29 '18 at 13:25
add a comment |
As an example, given a list xs = [1..10]
, the thing I want is:
[(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9),(9,10),(10,1)]
my solution is
zip xs (tail xs ++ [head xs]) -- solution (1)
and someone suggests that
zip xs (tail . cycle $ xs) -- solution (2)
but I don't know whether the solution (2) is more efficient? or two solutions are equivalent?
haskell
As an example, given a list xs = [1..10]
, the thing I want is:
[(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9),(9,10),(10,1)]
my solution is
zip xs (tail xs ++ [head xs]) -- solution (1)
and someone suggests that
zip xs (tail . cycle $ xs) -- solution (2)
but I don't know whether the solution (2) is more efficient? or two solutions are equivalent?
haskell
haskell
asked Dec 29 '18 at 13:12
JoeChoiJoeChoi
749
749
Have you tried timing the two methods and seeing for yourself?
– AJFarmar
Dec 29 '18 at 13:15
@AJFarmar I have not learned how to timing the Haskell program.
– JoeChoi
Dec 29 '18 at 13:18
2
I would expect these solutions to be similar in performance. One could try thecriterion
library to benchmark them. serpentine.com/criterion
– chi
Dec 29 '18 at 13:23
@chi Thank you very much. I shall try it later.
– JoeChoi
Dec 29 '18 at 13:25
add a comment |
Have you tried timing the two methods and seeing for yourself?
– AJFarmar
Dec 29 '18 at 13:15
@AJFarmar I have not learned how to timing the Haskell program.
– JoeChoi
Dec 29 '18 at 13:18
2
I would expect these solutions to be similar in performance. One could try thecriterion
library to benchmark them. serpentine.com/criterion
– chi
Dec 29 '18 at 13:23
@chi Thank you very much. I shall try it later.
– JoeChoi
Dec 29 '18 at 13:25
Have you tried timing the two methods and seeing for yourself?
– AJFarmar
Dec 29 '18 at 13:15
Have you tried timing the two methods and seeing for yourself?
– AJFarmar
Dec 29 '18 at 13:15
@AJFarmar I have not learned how to timing the Haskell program.
– JoeChoi
Dec 29 '18 at 13:18
@AJFarmar I have not learned how to timing the Haskell program.
– JoeChoi
Dec 29 '18 at 13:18
2
2
I would expect these solutions to be similar in performance. One could try the
criterion
library to benchmark them. serpentine.com/criterion– chi
Dec 29 '18 at 13:23
I would expect these solutions to be similar in performance. One could try the
criterion
library to benchmark them. serpentine.com/criterion– chi
Dec 29 '18 at 13:23
@chi Thank you very much. I shall try it later.
– JoeChoi
Dec 29 '18 at 13:25
@chi Thank you very much. I shall try it later.
– JoeChoi
Dec 29 '18 at 13:25
add a comment |
2 Answers
2
active
oldest
votes
Unexpected! Solution (1) is faster. I just test it using GHC Runtime System statistics. The test case is [1..10^7]
, here is code:
Solution (1):
xs = [1..10^7]
main = print $ last $ zip xs (tail xs ++ [head xs])
Solution (2):
xs = [1..10^7]
main = print $ last $ zip xs (tail . cycle $ xs )
Compile option:
ghc -O2 -rtsopts ZipCycleList1.hs
Run option:
ZipCycleList1.exe +RTS -s
Result of Solution (1):
1,520,081,768 bytes allocated in the heap
603,912 bytes copied during GC
42,960 bytes maximum residency (2 sample(s))
26,672 bytes maximum slop
2 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1459 colls, 0 par 0.000s 0.004s 0.0000s 0.0006s
Gen 1 2 colls, 0 par 0.000s 0.000s 0.0001s 0.0002s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.312s ( 0.305s elapsed)
GC time 0.000s ( 0.004s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 0.312s ( 0.310s elapsed)
%GC time 0.0% (1.3% elapsed)
Alloc rate 4,872,025,717 bytes per MUT second
Productivity 100.0% of total user, 98.5% of total elapsed
Result of Solution (2):
1,520,081,832 bytes allocated in the heap
992,426,304 bytes copied during GC
250,935,040 bytes maximum residency (12 sample(s))
42,981,632 bytes maximum slop
569 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1449 colls, 0 par 0.296s 0.301s 0.0002s 0.0006s
Gen 1 12 colls, 0 par 0.406s 0.622s 0.0518s 0.2284s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.328s ( 0.305s elapsed)
GC time 0.702s ( 0.922s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 1.030s ( 1.228s elapsed)
%GC time 68.2% (75.1% elapsed)
Alloc rate 4,640,024,688 bytes per MUT second
Productivity 31.8% of total user, 24.9% of total elapsed
add a comment |
My intuition is that they will have identical performance. If you prefer an empirical answer to one based on experience, then you should build yourself a small benchmark; the criterion or timeit packages are popular choices here. Be sure to compile and use -O2
, because the interpreter has famously unreliable performance and GHC's optimizer is very clever.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53969869%2fzip-cycle-list-which-way-is-efficient%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Unexpected! Solution (1) is faster. I just test it using GHC Runtime System statistics. The test case is [1..10^7]
, here is code:
Solution (1):
xs = [1..10^7]
main = print $ last $ zip xs (tail xs ++ [head xs])
Solution (2):
xs = [1..10^7]
main = print $ last $ zip xs (tail . cycle $ xs )
Compile option:
ghc -O2 -rtsopts ZipCycleList1.hs
Run option:
ZipCycleList1.exe +RTS -s
Result of Solution (1):
1,520,081,768 bytes allocated in the heap
603,912 bytes copied during GC
42,960 bytes maximum residency (2 sample(s))
26,672 bytes maximum slop
2 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1459 colls, 0 par 0.000s 0.004s 0.0000s 0.0006s
Gen 1 2 colls, 0 par 0.000s 0.000s 0.0001s 0.0002s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.312s ( 0.305s elapsed)
GC time 0.000s ( 0.004s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 0.312s ( 0.310s elapsed)
%GC time 0.0% (1.3% elapsed)
Alloc rate 4,872,025,717 bytes per MUT second
Productivity 100.0% of total user, 98.5% of total elapsed
Result of Solution (2):
1,520,081,832 bytes allocated in the heap
992,426,304 bytes copied during GC
250,935,040 bytes maximum residency (12 sample(s))
42,981,632 bytes maximum slop
569 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1449 colls, 0 par 0.296s 0.301s 0.0002s 0.0006s
Gen 1 12 colls, 0 par 0.406s 0.622s 0.0518s 0.2284s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.328s ( 0.305s elapsed)
GC time 0.702s ( 0.922s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 1.030s ( 1.228s elapsed)
%GC time 68.2% (75.1% elapsed)
Alloc rate 4,640,024,688 bytes per MUT second
Productivity 31.8% of total user, 24.9% of total elapsed
add a comment |
Unexpected! Solution (1) is faster. I just test it using GHC Runtime System statistics. The test case is [1..10^7]
, here is code:
Solution (1):
xs = [1..10^7]
main = print $ last $ zip xs (tail xs ++ [head xs])
Solution (2):
xs = [1..10^7]
main = print $ last $ zip xs (tail . cycle $ xs )
Compile option:
ghc -O2 -rtsopts ZipCycleList1.hs
Run option:
ZipCycleList1.exe +RTS -s
Result of Solution (1):
1,520,081,768 bytes allocated in the heap
603,912 bytes copied during GC
42,960 bytes maximum residency (2 sample(s))
26,672 bytes maximum slop
2 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1459 colls, 0 par 0.000s 0.004s 0.0000s 0.0006s
Gen 1 2 colls, 0 par 0.000s 0.000s 0.0001s 0.0002s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.312s ( 0.305s elapsed)
GC time 0.000s ( 0.004s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 0.312s ( 0.310s elapsed)
%GC time 0.0% (1.3% elapsed)
Alloc rate 4,872,025,717 bytes per MUT second
Productivity 100.0% of total user, 98.5% of total elapsed
Result of Solution (2):
1,520,081,832 bytes allocated in the heap
992,426,304 bytes copied during GC
250,935,040 bytes maximum residency (12 sample(s))
42,981,632 bytes maximum slop
569 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1449 colls, 0 par 0.296s 0.301s 0.0002s 0.0006s
Gen 1 12 colls, 0 par 0.406s 0.622s 0.0518s 0.2284s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.328s ( 0.305s elapsed)
GC time 0.702s ( 0.922s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 1.030s ( 1.228s elapsed)
%GC time 68.2% (75.1% elapsed)
Alloc rate 4,640,024,688 bytes per MUT second
Productivity 31.8% of total user, 24.9% of total elapsed
add a comment |
Unexpected! Solution (1) is faster. I just test it using GHC Runtime System statistics. The test case is [1..10^7]
, here is code:
Solution (1):
xs = [1..10^7]
main = print $ last $ zip xs (tail xs ++ [head xs])
Solution (2):
xs = [1..10^7]
main = print $ last $ zip xs (tail . cycle $ xs )
Compile option:
ghc -O2 -rtsopts ZipCycleList1.hs
Run option:
ZipCycleList1.exe +RTS -s
Result of Solution (1):
1,520,081,768 bytes allocated in the heap
603,912 bytes copied during GC
42,960 bytes maximum residency (2 sample(s))
26,672 bytes maximum slop
2 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1459 colls, 0 par 0.000s 0.004s 0.0000s 0.0006s
Gen 1 2 colls, 0 par 0.000s 0.000s 0.0001s 0.0002s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.312s ( 0.305s elapsed)
GC time 0.000s ( 0.004s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 0.312s ( 0.310s elapsed)
%GC time 0.0% (1.3% elapsed)
Alloc rate 4,872,025,717 bytes per MUT second
Productivity 100.0% of total user, 98.5% of total elapsed
Result of Solution (2):
1,520,081,832 bytes allocated in the heap
992,426,304 bytes copied during GC
250,935,040 bytes maximum residency (12 sample(s))
42,981,632 bytes maximum slop
569 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1449 colls, 0 par 0.296s 0.301s 0.0002s 0.0006s
Gen 1 12 colls, 0 par 0.406s 0.622s 0.0518s 0.2284s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.328s ( 0.305s elapsed)
GC time 0.702s ( 0.922s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 1.030s ( 1.228s elapsed)
%GC time 68.2% (75.1% elapsed)
Alloc rate 4,640,024,688 bytes per MUT second
Productivity 31.8% of total user, 24.9% of total elapsed
Unexpected! Solution (1) is faster. I just test it using GHC Runtime System statistics. The test case is [1..10^7]
, here is code:
Solution (1):
xs = [1..10^7]
main = print $ last $ zip xs (tail xs ++ [head xs])
Solution (2):
xs = [1..10^7]
main = print $ last $ zip xs (tail . cycle $ xs )
Compile option:
ghc -O2 -rtsopts ZipCycleList1.hs
Run option:
ZipCycleList1.exe +RTS -s
Result of Solution (1):
1,520,081,768 bytes allocated in the heap
603,912 bytes copied during GC
42,960 bytes maximum residency (2 sample(s))
26,672 bytes maximum slop
2 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1459 colls, 0 par 0.000s 0.004s 0.0000s 0.0006s
Gen 1 2 colls, 0 par 0.000s 0.000s 0.0001s 0.0002s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.312s ( 0.305s elapsed)
GC time 0.000s ( 0.004s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 0.312s ( 0.310s elapsed)
%GC time 0.0% (1.3% elapsed)
Alloc rate 4,872,025,717 bytes per MUT second
Productivity 100.0% of total user, 98.5% of total elapsed
Result of Solution (2):
1,520,081,832 bytes allocated in the heap
992,426,304 bytes copied during GC
250,935,040 bytes maximum residency (12 sample(s))
42,981,632 bytes maximum slop
569 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1449 colls, 0 par 0.296s 0.301s 0.0002s 0.0006s
Gen 1 12 colls, 0 par 0.406s 0.622s 0.0518s 0.2284s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.328s ( 0.305s elapsed)
GC time 0.702s ( 0.922s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 1.030s ( 1.228s elapsed)
%GC time 68.2% (75.1% elapsed)
Alloc rate 4,640,024,688 bytes per MUT second
Productivity 31.8% of total user, 24.9% of total elapsed
answered Dec 29 '18 at 17:52
assembly.jcassembly.jc
1,9681214
1,9681214
add a comment |
add a comment |
My intuition is that they will have identical performance. If you prefer an empirical answer to one based on experience, then you should build yourself a small benchmark; the criterion or timeit packages are popular choices here. Be sure to compile and use -O2
, because the interpreter has famously unreliable performance and GHC's optimizer is very clever.
add a comment |
My intuition is that they will have identical performance. If you prefer an empirical answer to one based on experience, then you should build yourself a small benchmark; the criterion or timeit packages are popular choices here. Be sure to compile and use -O2
, because the interpreter has famously unreliable performance and GHC's optimizer is very clever.
add a comment |
My intuition is that they will have identical performance. If you prefer an empirical answer to one based on experience, then you should build yourself a small benchmark; the criterion or timeit packages are popular choices here. Be sure to compile and use -O2
, because the interpreter has famously unreliable performance and GHC's optimizer is very clever.
My intuition is that they will have identical performance. If you prefer an empirical answer to one based on experience, then you should build yourself a small benchmark; the criterion or timeit packages are popular choices here. Be sure to compile and use -O2
, because the interpreter has famously unreliable performance and GHC's optimizer is very clever.
answered Dec 29 '18 at 14:44
Daniel WagnerDaniel Wagner
101k7158278
101k7158278
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53969869%2fzip-cycle-list-which-way-is-efficient%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you tried timing the two methods and seeing for yourself?
– AJFarmar
Dec 29 '18 at 13:15
@AJFarmar I have not learned how to timing the Haskell program.
– JoeChoi
Dec 29 '18 at 13:18
2
I would expect these solutions to be similar in performance. One could try the
criterion
library to benchmark them. serpentine.com/criterion– chi
Dec 29 '18 at 13:23
@chi Thank you very much. I shall try it later.
– JoeChoi
Dec 29 '18 at 13:25