Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Generally speaking, @m09's answer@m09's answer is basically right about the importance of tail-recursion.

For big N, calculating the product differently wins! Think "binary tree", not "linear list"...

Let's try both ways and compare the runtimes. First, @m09's factorial/2:

 ?- time((factorial(100000,_),false)). % 200,004 inferences, 1.606 CPU in 1.606 seconds (100% CPU, 124513 Lips) false. 

Next, we do it tree-style—using reduce/3reduce/3 together with lambda expressions:

 ?- time((numlist(1,100000,Xs),reduce(\X^Y^XY^(XY is X*Y),Xs,_),false)). % 1,300,042 inferences, 0.264 CPU in 0.264 seconds (100% CPU, 4922402 Lips) false. 

Last, let's define and use dedicated auxiliary predicate x_y_product/3:

x_y_product(X, Y, XY) :- XY is X*Y. 

What's to gain? Let's ask the stopwatch!

 ?- time((numlist(1,100000,Xs),reduce(x_y_product,Xs,_),false)). % 500,050 inferences, 0.094 CPU in 0.094 seconds (100% CPU, 5325635 Lips) false. 

Generally speaking, @m09's answer is basically right about the importance of tail-recursion.

For big N, calculating the product differently wins! Think "binary tree", not "linear list"...

Let's try both ways and compare the runtimes. First, @m09's factorial/2:

 ?- time((factorial(100000,_),false)). % 200,004 inferences, 1.606 CPU in 1.606 seconds (100% CPU, 124513 Lips) false. 

Next, we do it tree-style—using reduce/3 together with lambda expressions:

 ?- time((numlist(1,100000,Xs),reduce(\X^Y^XY^(XY is X*Y),Xs,_),false)). % 1,300,042 inferences, 0.264 CPU in 0.264 seconds (100% CPU, 4922402 Lips) false. 

Last, let's define and use dedicated auxiliary predicate x_y_product/3:

x_y_product(X, Y, XY) :- XY is X*Y. 

What's to gain? Let's ask the stopwatch!

 ?- time((numlist(1,100000,Xs),reduce(x_y_product,Xs,_),false)). % 500,050 inferences, 0.094 CPU in 0.094 seconds (100% CPU, 5325635 Lips) false. 

Generally speaking, @m09's answer is basically right about the importance of tail-recursion.

For big N, calculating the product differently wins! Think "binary tree", not "linear list"...

Let's try both ways and compare the runtimes. First, @m09's factorial/2:

 ?- time((factorial(100000,_),false)). % 200,004 inferences, 1.606 CPU in 1.606 seconds (100% CPU, 124513 Lips) false. 

Next, we do it tree-style—using reduce/3 together with lambda expressions:

 ?- time((numlist(1,100000,Xs),reduce(\X^Y^XY^(XY is X*Y),Xs,_),false)). % 1,300,042 inferences, 0.264 CPU in 0.264 seconds (100% CPU, 4922402 Lips) false. 

Last, let's define and use dedicated auxiliary predicate x_y_product/3:

x_y_product(X, Y, XY) :- XY is X*Y. 

What's to gain? Let's ask the stopwatch!

 ?- time((numlist(1,100000,Xs),reduce(x_y_product,Xs,_),false)). % 500,050 inferences, 0.094 CPU in 0.094 seconds (100% CPU, 5325635 Lips) false. 
layout, predicate name
Source Link
repeat
  • 18.8k
  • 4
  • 57
  • 166

Generally speaking, @m09's answer is basically right about the importance of tail-recursion.

For big N, calculating the product differently wins! Think "binary tree", not "linear list"...

Let's try both ways and compare the runtimes. First, @m09's factorial/2:

 ?- time((factorial(100000,_),false)). % 200,004 inferences, 1.606 CPU in 1.606 seconds (100% CPU, 124513 Lips) false. 

Next, we do it tree-style---by usingstyle—using reduce/3 together with lambda expressions:

 ?- time((numlist(1,100000,Xs),reduce(\X^Y^XY^(XY is X*Y),Xs,_),false)). % 1,300,042 inferences, 0.264 CPU in 0.264 seconds (100% CPU, 4922402 Lips) false. 

Last, let's define and use dedicated auxiliary predicate num_num_productx_y_product/3:

num_num_productx_y_product(X, Y, XY) :-  XY is X*Y. 

What's to gain? Let's ask the stopwatch!

 ?- time((numlist(1,100000,Xs),reduce(num_num_productx_y_product,Xs,_),false)). % 500,050 inferences, 0.094 CPU in 0.094 seconds (100% CPU, 5325635 Lips) false. 

Generally speaking, @m09's answer is basically right about the importance of tail-recursion.

For big N, calculating the product differently wins! Think "binary tree", not "linear list"...

Let's try both ways and compare the runtimes. First, @m09's factorial/2:

 ?- time((factorial(100000,_),false)). % 200,004 inferences, 1.606 CPU in 1.606 seconds (100% CPU, 124513 Lips) false. 

Next, we do it tree-style---by using reduce/3 together with lambda expressions:

 ?- time((numlist(1,100000,Xs),reduce(\X^Y^XY^(XY is X*Y),Xs,_),false)). % 1,300,042 inferences, 0.264 CPU in 0.264 seconds (100% CPU, 4922402 Lips) false. 

Last, let's define and use dedicated auxiliary predicate num_num_product/3:

num_num_product(X,Y,XY) :-  XY is X*Y. 

What's to gain? Let's ask the stopwatch!

 ?- time((numlist(1,100000,Xs),reduce(num_num_product,Xs,_),false)). % 500,050 inferences, 0.094 CPU in 0.094 seconds (100% CPU, 5325635 Lips) false. 

Generally speaking, @m09's answer is basically right about the importance of tail-recursion.

For big N, calculating the product differently wins! Think "binary tree", not "linear list"...

Let's try both ways and compare the runtimes. First, @m09's factorial/2:

 ?- time((factorial(100000,_),false)). % 200,004 inferences, 1.606 CPU in 1.606 seconds (100% CPU, 124513 Lips) false. 

Next, we do it tree-style—using reduce/3 together with lambda expressions:

 ?- time((numlist(1,100000,Xs),reduce(\X^Y^XY^(XY is X*Y),Xs,_),false)). % 1,300,042 inferences, 0.264 CPU in 0.264 seconds (100% CPU, 4922402 Lips) false. 

Last, let's define and use dedicated auxiliary predicate x_y_product/3:

x_y_product(X, Y, XY) :- XY is X*Y. 

What's to gain? Let's ask the stopwatch!

 ?- time((numlist(1,100000,Xs),reduce(x_y_product,Xs,_),false)). % 500,050 inferences, 0.094 CPU in 0.094 seconds (100% CPU, 5325635 Lips) false. 
Source Link
repeat
  • 18.8k
  • 4
  • 57
  • 166

Generally speaking, @m09's answer is basically right about the importance of tail-recursion.

For big N, calculating the product differently wins! Think "binary tree", not "linear list"...

Let's try both ways and compare the runtimes. First, @m09's factorial/2:

 ?- time((factorial(100000,_),false)). % 200,004 inferences, 1.606 CPU in 1.606 seconds (100% CPU, 124513 Lips) false. 

Next, we do it tree-style---by using reduce/3 together with lambda expressions:

 ?- time((numlist(1,100000,Xs),reduce(\X^Y^XY^(XY is X*Y),Xs,_),false)). % 1,300,042 inferences, 0.264 CPU in 0.264 seconds (100% CPU, 4922402 Lips) false. 

Last, let's define and use dedicated auxiliary predicate num_num_product/3:

num_num_product(X,Y,XY) :- XY is X*Y. 

What's to gain? Let's ask the stopwatch!

 ?- time((numlist(1,100000,Xs),reduce(num_num_product,Xs,_),false)). % 500,050 inferences, 0.094 CPU in 0.094 seconds (100% CPU, 5325635 Lips) false.