Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here is one way to make it 30x 150x faster using your 5000 random number bench. Kinda cheating of course and still room for more optimisation.

[root@devel3 root]# time ./trinum.pl >out real 0m41.310s user 0m41.310s sys 0m0.000s [root@devel3 root]# time ./trinum_c2.pl >out real 0m0.276s user 0m0.280s sys 0m0.000s [root@devel3 root]# more trinum_c2.pl #!/usr/bin/perl use Inline 'C'; my $file = $ARGV[0] || 'targets.txt'; open (INPUT, '<', $file) or die "Unable to open $file for reading : $! +"; while ( <INPUT> ) { chomp; get_three( $_ ); } __END__ __C__ #include <math.h> #define TRI(i) (i*(i+1)/2) int get_three ( int num ) { register max,target,i,j; max = (int)sqrt((double)(2*num)); while ( max != 0 ) { target = num - TRI(max); for( i=0;TRI(i)<=target;i++ ) { for( j=0;j<=i;j++ ) { if ( target == (int)(TRI(i)+TRI(j)) ) { printf( "%d, %d, %d\n", TRI(max), TRI(i), TRI(j) ) +; return 1; } } } max--; } printf( "Something went horribly wrong : %d\n", num ); return 0; } [root@devel3 root]#

This was the original try which was only 30x faster and took over a whole second to run :-)

[root@devel3 root]# time ./trinum_c.pl >out_c real 0m1.158s user 0m1.150s sys 0m0.000s [root@devel3 root]# cat trinum_c.pl #!/usr/bin/perl use Inline 'C'; my $file = $ARGV[0] || 'targets.txt'; open (INPUT, '<', $file) or die "Unable to open $file for reading : $! +"; while ( <INPUT> ) { chomp; get_three( $_ ); } __END__ __C__ #include <math.h> int p_tri( int num ) { double x; int t; x = ( sqrt( 8 * (double)num + 1 ) + 1 )/ 2; t = (int)x; return (double)t == x ? 0 : t; } int get_three ( int num ) { register prev,i,j,tri; if ( ! (prev = p_tri( num )) ) { printf( "%d, 0, 0\n", num ); return 1; } while ( --prev ) { for( tri=prev*(prev+1)/2,i=0,j=num-tri; j>=i; j--,i++ ) { if ( 0==p_tri(i) && 0==p_tri(j) ) { printf( "%d, %d, %d\n", tri, i, j ); return 1; } } } printf( "Something went horribly wrong : %d\n", num ); return 0; } [root@devel3 root]#

cheers

tachyon


In reply to Re: Triangle Numbers Revisited by tachyon
in thread Triangle Numbers Revisited by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-23 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found