Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

new DLL compile code inside script languages

by orange (Beadle)
on Jul 04, 2009 at 13:35 UTC ( [id://777231]=perlquestion: print w/replies, xml ) Need Help??

orange has asked for the wisdom of the Perl Monks concerning the following question:

Hi
i have found a dll special for script languages, the author said:
O2H is a Just-In-Time compiler assembler, deployed in a single DLL that takes source code strings and returns executable binary. the author said that he managed it to be a general dll. i am trying to use it in perl, you can download it from here:
http://community.thinbasic.com/index.php?topic=2517.0 just download the file : thinBasic_Oxygen.zip
to give you some idea first:
in perl to sum the numbers from 1 to 100000000 (one hundred million) such as:
my $a; for( $a=1; $a<100000001; $a=$a+1 ) { $t = $t + $a; } print $t;
it takes about 65 seconds to give the result 5.00000005e+015 = 5000000050000000
while in vb6 calling this dll it is less than 2 seconds to give the result 5000000050000000
i have tried it with visual basic 6 as follows:
the full vb6 source with the dll here:
http://sites.google.com/site/zak31415/Home/vb6_oxygen2.rar first: i have declared its functions in a vb6 Module:
Declare Sub o2_asmo Lib "thinbasic_oxygen.dll" (ByVal s As String) Declare Sub o2_basic Lib "thinbasic_oxygen.dll" (ByVal s As String) Declare Function o2_buf Lib "thinbasic_oxygen.dll" (ByVal n As Long) A +s Long Declare Function o2_error Lib "thinbasic_oxygen.dll" () As String Declare Function o2_exec Lib "thinbasic_oxygen.dll" (Optional ByVal p +As Long) As Long Declare Function o2_get Lib "thinbasic_oxygen.dll" Alias "o2_buf" () A +s String Declare Function o2_len Lib "thinbasic_oxygen.dll" () As Long Declare Function o2_prep Lib "thinbasic_oxygen.dll" (ByVal srcBSTR As +String) As String Declare Sub o2_put Lib "thinbasic_oxygen.dll" Alias "o2_buf" (ByVal c +As String) Declare Function o2_view Lib "thinbasic_oxygen.dll" (ByVal srcBSTR As +String) As String
second: i have write in a form the following code:
Private Sub Command1_Click() Dim src As String src = " dim a as quad : dim i as long: for i=1 to 100000000: a=a+i : n +ext i: Print str a: mov eax,a :terminate " o2_basic src o2_exec End Sub
just compile the code first and run the compiled code
now in perl: i have great difficulties with api
#! perl -slw use Win32::API; my $o2basic = new Win32::API( "thinBasic_Oxygen.dll","o2_basic", [P], +P ); my $o2exec = new Win32::API( "thinBasic_Oxygen.dll","o2_exec", [P] ); my $src = " dim a as quad : dim i as long: for i=1 to 100000000: a=a+i + : next i: Print str a: mov eax,a :terminate "; $o2basic->Call( $src ); #gives an error here #what should i do here with o2exec ??: #$o2exec->??;
to execute the function o2basic it gives an error, try it please. ps.. the instruction mov eax,a are not used here but it is to make it possible to pass the result to vb6 variable such as v = o2exec thanks

Replies are listed 'Best First'.
Re: new DLL compile code inside script languages
by CountZero (Bishop) on Jul 04, 2009 at 22:25 UTC
    perl -e "$sum += $_ for (1 .. 100000000); print $sum;"
    took about 26 seconds on my Windows XP Pro with an Intel Pentium 1.7 Ghz processor.

    Fast enough for me and it took me like 10 seconds to write this code. I see no benefit to code it in PDL or call arcane functions in a DLL: that would have taken me much much longer.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Of course in Linux you want to change those double " to single ': perl -e '$sum += $_ for (1 .. 100000000); print $sum;'
Re: new DLL compile code inside script languages
by Anonymous Monk on Jul 04, 2009 at 14:32 UTC
    PDL should be faster (after compilation)
    #!/usr/bin/perl -- use strict; use warnings; use PDL; use Inline qw( Pdlpp ); print pdl(1)->eight,"\n"; print pdl(10)->eight,"\n"; print pdl(100)->eight,"\n"; print pdl(100000000 )->eight,"\n"; __DATA__ __Pdlpp__ pp_addhdr( ' double eight_sum(int n) { int i, sum = 0; for (i=1; i<=n; i++) { sum += i; } return sum; } ' ); pp_def( 'eight', Pars => 'int a(); int [o]b();', Code => '$b()=eight_sum($a());' ); pp_done();
    On my system
    time perl inline.pdlpp2.pl 1 55 5050 987459712 real 1.00 user 0.00 sys 0.00
      It takes 7seconds on first run to compile, after that its really fast
      $ call time perl inline.pdlpp2.pl 1 55 5050 987459712 real 7.00 user 0.00 sys 0.00 $ call time perl inline.pdlpp2.pl 1 55 5050 987459712 real 0.00 user 0.00 sys 0.00
Re: new DLL compile code inside script languages
by Jorge_de_Burgos (Beadle) on Jul 05, 2009 at 15:40 UTC
    This would be a minimum inline-C procedure in Perl. Very fast. Very short!
    #!/usr/bin/perl use Inline C; print suma(100_000_000); __END__ __C__ double suma(double n) { double i, sum = 0; for (i=1; i<=n; i++) { sum += i; } return sum; }
        Listen roboticus, we are comparing brute-force approaches here...
Re: new DLL compile code inside script languages
by Anonymous Monk on Jul 04, 2009 at 13:47 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://777231]
Approved by ww
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-24 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found