Page 3 of 11 FirstFirst 12345 ... LastLast
Results 21 to 30 of 105

Thread: Prime Time: C, Lisp, and Python

  1. #21
    Join Date
    Dec 2008
    Location
    USA, Florida
    Beans
    402
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Prime Time: C, Lisp, and Python

    Quote Originally Posted by nvteighen View Post
    Ok, now my experiment worked... See my post above.
    I'm glad I chose to learn Lisp over Scheme.

  2. #22
    Join Date
    May 2009
    Location
    Grenoble, France
    Beans
    131
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Prime Time: C, Lisp, and Python

    Quote Originally Posted by JordyD View Post
    Neat. Maybe I should learn OCaml. (After Lisp, maybe)

    I await the day we find a higher-level language faster than C. I will be so happy.
    Well, I believe ocaml is certainly worth of learning.

    Talking about a language faster than C, it does not exists because in C the code is very close to direct machine code so you can write highly optimised code. FORTRAN can be faster then C for some number crunching applications but the language is horrible so it is not a very interesting to learn.

    For the curious, here the assembler code generated with C for the check_prime function:
    Code:
    check_prime:
            pushl   %esi
            pushl   %ebx
            movl    12(%esp), %esi
            movl    %esi, %eax
            shrl    $31, %eax
            leal    (%eax,%esi), %ebx
            sarl    %ebx
            cmpl    $1, %ebx
            jle     .L2
            movl    %eax, %edx
            movl    $2, %ecx
            leal    (%esi,%eax), %eax
            andl    $1, %eax
            cmpl    %edx, %eax
            jne     .L4
            jmp     .L3
            .p2align 4,,7
            .p2align 3
    .L5:
            movl    %esi, %edx
           movl    %esi, %eax
            sarl    $31, %edx
            idivl   %ecx
            testl   %edx, %edx
            je      .L3
    .L4:
            addl    $1, %ecx
            cmpl    %ecx, %ebx
            jge     .L5
    .L2:
            movl    $1, %eax
            popl    %ebx
            popl    %esi
            ret
            .p2align 4,,7
            .p2align 3
    .L3:
            xorl    %eax, %eax
            popl    %ebx
            popl    %esi
            ret
    and by ocaml for the check_prime_rec function:
    Code:
    camlMlprime__check_prime_rec_60:
    .L104:
            movl    %eax, %edi
            movl    %ecx, %esi
            cmpl    %esi, %ebx
            jle     .L103
            movl    $3, %eax
            ret
            .align  16
    .L103:
            movl    %ebx, %ecx
            sarl    $1, %ecx
            testl   %ecx, %ecx
            je      .L102
            movl    %edi, %eax
            sarl    $1, %eax
            cltd
            idivl   %ecx
            jmp     .L101
            .align  16
    .L102:
           movl    $caml_bucket_Division_by_zero, %eax
            movl    caml_exception_pointer, %esp
            popl    caml_exception_pointer
            ret
            .align  16
    .L101:
            sall    $1, %edx
            incl    %edx
            cmpl    $1, %edx
            jne     .L100
            movl    $1, %eax
            ret
            .align  16
    .L100:
            addl    $2, %ebx
            movl    %edi, %eax
            movl    %esi, %ecx
            jmp     .L104
    As you can see they are absolutely comparable and ocaml is also checking the division by zero and it raise an exception if it happens.

    Francesco

  3. #23
    Join Date
    May 2009
    Location
    Grenoble, France
    Beans
    131
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Prime Time: C, Lisp, and Python

    Quote Originally Posted by JordyD View Post
    EDIT2: I get these results on my system:
    C:
    Code:
    real    0m0.285s
    user    0m0.084s
    sys    0m0.016s
    OCaml:
    Code:
    real    0m2.627s
    user    0m2.436s
    sys    0m0.016s
    Very strange. You are sure you have used ocamlopt to compile the ocaml code ? you should not use ocamlc because it does produce interpreted bytecode.

  4. #24
    Join Date
    Dec 2008
    Location
    USA, Florida
    Beans
    402
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Prime Time: C, Lisp, and Python

    Quote Originally Posted by fr4nko View Post
    Very strange. You are sure you have used ocamlopt to compile the ocaml code ? you should not use ocamlc because it does produce interpreted bytecode.
    I did use ocamlopt. Perhaps it does not like my CPU? It also jumps when it runs. I get results in chunks.

    I will test it on my other computer.

  5. #25
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,440

    Re: Prime Time: C, Lisp, and Python

    Quote Originally Posted by JordyD View Post
    I'm glad I chose to learn Lisp over Scheme.
    Scheme is a Lisp... but, the point of Scheme is not this sort of stuff, it's the ideas in it instead.

    Anyway, a couple of things... loading the lisp image probably adds a lot to your runtime... you may want to test using the built-in "time" function:

    Code:
    (time (some-operation))
    Second, look up in the docs for the ways to explicitly declare your variable types. This way, a simple numeric algorithm like this ought to essentially compile into pretty much the same machine code you'd get from the C version. (You can even check with "disassemble").
    LambdaGrok. | #ubuntu-programming on FreeNode

  6. #26

    Re: Prime Time: C, Lisp, and Python

    Quote Originally Posted by JordyD View Post
    I'm glad I chose to learn Lisp over Scheme.
    Scheme *is* a dialect of Lisp, Like Common Lisp, Clojure etc.
    im dyslexic, please don't comment on my spelling
    blender 3d artist, visit my portfolio
    Quad-Ren, Open source, resolution independent 2D graphics engine
    Screen space is a precious resource, don't waste it

  7. #27
    Join Date
    Dec 2008
    Location
    USA, Florida
    Beans
    402
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Prime Time: C, Lisp, and Python

    Quote Originally Posted by CptPicard View Post
    Scheme is a Lisp... but, the point of Scheme is not this sort of stuff, it's the ideas in it instead.

    Anyway, a couple of things... loading the lisp image probably adds a lot to your runtime... you may want to test using the built-in "time" function:

    Code:
    (time (some-operation))
    Second, look up in the docs for the ways to explicitly declare your variable types. This way, a simple numeric algorithm like this ought to essentially compile into pretty much the same machine code you'd get from the C version. (You can even check with "disassemble").
    Neat. I'll try that and post the results when done.

    About the built-in time function:
    I was aware of it, but I wanted to see how they fared as stand-alone executables. Granted I didn't do that with python (need an interpreter), but in all fairness, I can still do a ./prime.py, where with lisp I would have to do an sbcl --load prime.lisp.

    Quote Originally Posted by hessiess View Post
    Scheme *is* a dialect of Lisp, Like Common Lisp, Clojure etc.
    Well, yes, I was aware that it's a dialect of Lisp, but I am still happier that I learned Common Lisp instead of Scheme.
    Last edited by JordyD; July 17th, 2009 at 10:09 PM.

  8. #28
    Join Date
    Dec 2008
    Location
    USA, Florida
    Beans
    402
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Prime Time: C, Lisp, and Python

    Quote Originally Posted by JordyD View Post
    I did use ocamlopt. Perhaps it does not like my CPU? It also jumps when it runs. I get results in chunks.

    I will test it on my other computer.
    Same results on my other computer.

  9. #29
    Join Date
    Dec 2006
    Beans
    65

    Re: Prime Time: C, Lisp, and Python

    Quote Originally Posted by Can+~ View Post
    def primegen(max):

    test = lambda x: x%2 and x%3 and x%5 and x%7
    code
    This algorithm is wrong; you cannot test if a number is prime by only dividing it by 2, 3, 5 and 7. Your program will say that 121 = 11 * 11 is prime since it is not divisible by 2, 3, 5 or 7.

  10. #30
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Prime Time: C, Lisp, and Python

    Quote Originally Posted by cyberslayer View Post
    This algorithm is wrong; you cannot test if a number is prime by only dividing it by 2, 3, 5 and 7. Your program will say that 121 = 11 * 11 is prime since it is not divisible by 2, 3, 5 or 7.
    That's why I said I had an error. (Added a *edit2 to make it clear)

    The reason of that mistake was that, back then, I used the Sieve of Eratosthenes for the <100; and now I just upped the maximum.
    Last edited by Can+~; July 17th, 2009 at 10:44 PM.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

Page 3 of 11 FirstFirst 12345 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •