Comparison of Haskell, C #, Java, and C program execution efficiency: write your first parallel Haskell Program

Source: Internet
Author: User

From: http://www.haskell.org/haskellwiki/Haskell_in_5_steps#Write_your_first_parallel_Haskell_program

Starting from: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' challenge Q' 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 = m-1) 1ack m n = m-1) (ack m (n-1) fib 0 = 0fib 1 = 1fib n = fib (n-1) + fib (n-2)

Compile and run:

-------- Multi-core version --------------------------------- $ ghc-O2-O mcore.exe -- make Mt. HS-threaded-rtsoptslinking mcore.exe... administrator @ spark D:/workspace/Haskell $ time mcore + RTS-n21405006117752879898543142606244511569936384005711076real 0m1. 812 suser 0m0. 000 SSYS 0m0. 031 s -------- Single-core version ------------------------------- 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. 406 suser 0m0. 000 SSYS 0m0. 000 s ----------------------------------------------------

Run inWin32 + git bash.

/*************************************** ****************************************/

Then, the execution efficiency in Java and C # is compared:

C # version:

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 = 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 ();}}}

Use release to compile and run the program. Double-click the program directly in the resource manager to run the program:

The running result of C # Release any CPU version is as follows:

1.40500611775288e + 51

Time elapsed: 00:00:00. 9070211

/*************************************** ****************************************/

Java version:

// 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) {e Lapsed = (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 = 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 ());}}

After the command line is compiled, the running result is about:

$ Java-xms512m-xmx1024m-xss100m Test

1.4050061177528798e51

Elapsed time in milliseconds: 843


Note: Run options. The default value is:

-Xms40m

-Xmx512m

Exception in thread "Main" Java. Lang. stackoverflowerror

At test. Ack (test. Java: 13)

Therefore, you need to change the VM options. /*************************************** ****************************************/ Use the C language GCC compiler
# 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 installation 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 run: D: Test \ CB \ comparelang \ bin \ release> in cmd. Timeit comparelang.exe 1405006117752880000000000000000000000000000000000000.000000 Version Number: Windows NT 5.2 (build 3790) Exit Time: Am, Friday, October 21 2011 Elapsed time: 0:00:00. 937 Time: 0:00:00. 937 System cballs: 12537 Context switch: 10635 Page faults: 466 Bytes read: 2564 Bytes written: 2540 Bytes other: 2306

/*************************************** ****************************************/

Final result:

Java is superior.

C #. Net followed.

C third (unexpectedly, there was no Java, C # Fast)

Haskell multi-core version 4.

/*************************************** ****************************************/




Attached Java running option explanation:

Http://publib.boulder.ibm.com/infocenter/javasdk/tools/index.jsp? Bytes

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 cballs 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 CILS 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 initialNewSpace size-xmnx0k maximumNewSpace size-xms4m initial memory size-Xmos 4m initial old space size-memory maximum old space size-xmx1500m memory maximum-xmr16k remembered set size-xmso32k operating system thread stack size 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.