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 = 1fac n = n * fac (n-1) ack 0 n = n+1ack m 0 = ack (m-1) 1ack m n = ack (m-1) (ack m (n-1)) fib 0 = 0fib 1 = 1fib n = fib (n-1) + fib (n-2)
編譯運行:
--------多核版本-----------------------------------$ ghc -O2 -o mcore.exe --make mt.hs -threaded -rtsoptsLinking mcore.exe ...Administrator@SPARK d:/workspace/Haskell$ time mcore +RTS -N21405006117752879898543142606244511569936384005711076real 0m1.812suser 0m0.000ssys 0m0.031s--------單核版本-----------------------------------Administrator@SPARK d:/workspace/Haskell$ ghc -O2 -o score.exe --make mt.hs -rtsoptsLinking score.exe ...Administrator@SPARK d:/workspace/Haskell$ time score +RTS1405006117752879898543142606244511569936384005711076real 0m2.406suser 0m0.000ssys 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.javapublic 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.javapublic 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)
故需要更改VM選項。
/*******************************************************************************/再用C語言gcc編譯器
#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
Windows Server 2003 Resource Kit Tools
http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=17657在 cmd中執行:D:test\CB\CompareLang\bin\Release>
timeit CompareLang.exe
1405006117752880000000000000000000000000000000000000.000000
Version Number: Windows NT 5.2 (Build 3790)
Exit Time: 12:01 am, Friday, October 21 2011
Elapsed Time: 0:00:00.937
Process Time: 0:00:00.937
System Calls: 12537
Context Switches: 10635
Page Faults: 466
Bytes Read: 2564
Bytes Written: 2540
Bytes Other: 2306
/*******************************************************************************/
最終結果:
java 略勝一籌。
C#.net次之.
C 第三 (意外,居然沒有java,c#快)
Haskell多核版本第四。
/*******************************************************************************/
附java運行選項解釋:
http://publib.boulder.ibm.com/infocenter/javasdk/tools/index.jsp?topic=%2Fcom.ibm.java.doc.igaa%2F_1vg000131402ee-11e49e6e566-7fda_1001.html
Resolving a native stack overflow
Information to diagnose and resolve a stack overflow.
-
Introduction
-
The two main causes of stack overflow are as follows:
- Infinite recursion is occurring in the application.
- There is insufficient stack size for application.
Read the information below to determine which problem is occurring and how to resolve the issue.
-
Determining if the problem is infinite recursion or insufficient stack size
-
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.
-
Resolving the problem
-
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.