from: http://www.haskell.org/haskellwiki/Haskell_in_5_steps#Write_your_first_parallel_Haskell_program
起由: Write your first parallel Haskell program
Haskell has good support for parallel and multicore programming. We can write a parallel program by adding `par` to expressions, like so:
import Control.Parallel main = a `par` b `par` c `pseq` print (a + b + c) where a = ack 3 10 b = fac 42 c = fib 34 fac 0 = 1 fac n = n * fac (n-1) ack 0 n = n+1 ack m 0 = ack (m-1) 1 ack m n = ack (m-1) (ack m (n-1)) fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n-2)
编译运行:
--------多核版本----------------------------------- $ ghc -O2 -o mcore.exe --make mt.hs -threaded -rtsopts Linking mcore.exe ... Administrator@SPARK d:/workspace/Haskell $ time mcore +RTS -N2 1405006117752879898543142606244511569936384005711076 real 0m1.812s user 0m0.000s sys 0m0.031s --------单核版本----------------------------------- Administrator@SPARK d:/workspace/Haskell $ ghc -O2 -o score.exe --make mt.hs -rtsopts Linking score.exe ... Administrator@SPARK d:/workspace/Haskell $ time score +RTS 1405006117752879898543142606244511569936384005711076 real 0m2.406s user 0m0.000s sys 0m0.000s ----------------------------------------------------
运行在win32 + Git Bash。
/*******************************************************************************/
之后又在java和c#中执行效率相比较一下:
C#版本:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Diagnostics; namespace CompareMulticore { class Program { static double fac(double n) { return n == 0 ? 1 : n * fac(n - 1); } static double ack(double m, double n) { if (m == 0) return n + 1; if (n == 0) return ack(m - 1, 1); return ack(m - 1, ack(m,n-1)); } static double fib(double n) { if (n == 0 || n == 1) return n; return fib(n - 1) + fib(n - 2); } static void Main(string[] args) { // Create new stopwatch Stopwatch stopwatch = new Stopwatch(); // Begin timing stopwatch.Start(); // Do something double u1 = ack(3, 10); double u2 = fac(42); double u3 = fib(34); Console.WriteLine(u1 + u2 + u3); // Stop timing stopwatch.Stop(); // Write result Console.WriteLine("Time elapsed: {0}",stopwatch.Elapsed); Console.ReadKey(); } } }
采用release编译后运行,直接在资源管理器里双击运行:
C# Release Any CPU版本运行结果约为:
1.40500611775288E+51
Time elapsed: 00:00:00.9070211
/*******************************************************************************/
java版本:
//StopWatch.java public class StopWatch { private long startTime = 0; private long stopTime = 0; private boolean running = false; public void start() { this.startTime = System.currentTimeMillis(); this.running = true; } public void stop() { this.stopTime = System.currentTimeMillis(); this.running = false; } //elaspsed time in milliseconds public long getElapsedTime() { long elapsed; if (running) { elapsed = (System.currentTimeMillis() - startTime); } else { elapsed = (stopTime - startTime); } return elapsed; } //elaspsed time in seconds public long getElapsedTimeSecs() { long elapsed; if (running) { elapsed = ((System.currentTimeMillis() - startTime) / 1000); } else { elapsed = ((stopTime - startTime) / 1000); } return elapsed; } } //Test.java public class Test { static double fac(double n) { return n == 0 ? 1 : n * fac(n - 1); } static double ack(double m, double n) { if (m == 0) return n + 1; if (n == 0) return ack(m - 1, 1); return ack(m - 1, ack(m,n-1)); } static double fib(double n) { if (n == 0 || n == 1) return n; return fib(n - 1) + fib(n - 2); } /** * @param args */ public static void main(String[] args) { // Create new stopwatch StopWatch stopwatch = new StopWatch(); // Begin timing stopwatch.start(); // Do something double u1 = ack(3, 10); double u2 = fac(42); double u3 = fib(34); System.out.println(u1 + u2 + u3); // Stop timing stopwatch.stop(); System.out.println("elapsed time in milliseconds: " + stopwatch.getElapsedTime()); } }
命令行编绎后运行结果约为:
$ java -Xms512M -Xmx1024M -Xss100M Test
1.4050061177528798E51
elapsed time in milliseconds: 843
注意:运行选项,默认为:
-Xms40m
-Xmx512m 出现
Exception in thread "main" java.lang.StackOverflowError
at Test.ack(Test.java:13)
#include <stdio.h> #include <stdlib.h> #include <time.h> double fac(double n) { return n == 0 ? 1 : n * fac(n - 1); } double ack(double m, double n) { if (m == 0) return n + 1; if (n == 0) return ack(m - 1, 1); return ack(m - 1, ack(m,n-1)); } double fib(double n) { if (n == 0 || n == 1) return n; return fib(n - 1) + fib(n - 2); } int main() { //time_t start,end; //double dif; //time (&start); double u1 = ack(3, 10); double u2 = fac(42); double u3 = fib(34); printf("%f",u1 + u2 + u3); //time (&end); //printf ("\nIt took you %.2lf seconds to do this.\n", dif ); //printf("Press Enter to exit."); //getchar(); return 0; }
windows server 2003安装http://stackoverflow.com/questions/673523/how-to-measure-execution-time-of-command-in-windows-command-line
/*******************************************************************************/
最终结果:
java 略胜一筹。
C#.net次之.
C 第三 (意外,居然没有java,c#快)
Haskell多核版本第四。
/*******************************************************************************/
附java运行选项解释:
Information to diagnose and resolve a stack overflow.
The two main causes of stack overflow are as follows:
Read the information below to determine which problem is occurring and how to resolve the issue.
Infinite recursion is the most common cause of stack overflows. This problem occurs when a single method or function, or several methods or functions, are called repeatedly. The Just-In-Time (JIT) Compiler uses optimization to try to prevent a stack overflow when a single Java method is called repeatedly. The most common cause of infinite recursion in a Java application is the use of repeated patterns of Java methods or repeated calls to C functions.
To determine if infinite recursion has taken place, look at the stack trace for the thread that overflowed. In this way, you can identify repeating C function calls or patterns of Java methods. Use this information to check the repeated code for errors that might cause the infinite loop.
A large stack might be required if the repeated pattern of functions or methods is processing a large data artefact, such as an XML Document Object Model (DOM). In this case, increase the native stack size using the -Xmso command-line option in Java.
If you are unsure whether the problem is caused by infinite recursion or insufficient stack size, try increasing the native stack size to see if an increase prevents the problem.
Find the current stack size by running the following command line option to Java:
-verbose:sizes
This command prints out the value of various memory size settings. Here is the output for 32-bit Windows:
-Xmca32K RAM class segment increment -Xmco128K ROM class segment increment -Xmns0K initial new space size -Xmnx0K maximum new space size -Xms4M initial memory size -Xmos4M initial old space size -Xmox1500M maximum old space size -Xmx1500M memory maximum -Xmr16K remembered set size -Xmso32K operating system thread stack size -Xiss2K java thread stack initial size -Xssi16K java thread stack increment -Xss256K java thread stack maximum size
This output shows an operating system thread stack size of 32 KB. Increase this value by a factor of two using the -Xmso command-line option.
If the problem remains after increasing the stack size, infinite recursion is probably the cause. Ask the owner of the code to analyze it for potential problems. Meanwhile, run the application with larger native stack sizes.