Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

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

Edit: Added Inline::C version and quick'n'dirty Test::More tests.

Finding the next and previous newlines with index() and rindex() from the current $position will net you a good speedup (~200%), while a pure Inline::C solution will get you around ~600%. At the end of the day, you still have to scan left and right in the string. I included a very basic test framework, which highlights some unexpected cases in your version (op() in my example code).

This is about as fast as you can go without changing your algorithm, as scanning character by character for the next/previous newlines is going to be necessary no matter what.

Benchmark Results:

$> BENCHMARK=5 perl subline.pl Rate op tybalt89 daxim rlindex cur_s +trlen op 536066/s -- -0% -22% -66% + -86% tybalt89 538753/s 1% -- -22% -66% + -86% daxim 688615/s 28% 28% -- -57% + -82% rlindex 1585059/s 196% 194% 130% -- + -58% cur_strlen 3749674/s 599% 596% 445% 137% + --

As your subroutine didn't do any error or out of bounds checking, or define what happens if C<$position> is on a line boundary already, I did not do anything with those edge cases. You probably will, though.

Full code is in the readmore.

use Benchmark qw/cmpthese/; use Test::More; use experimental 'signatures'; # for daxim my $string ="test\nI want length of this line\n test"; my $position = 12; # Note this postion can be any position in any line +. # currently considering it inside 2nd line if ($ENV{BENCHMARK}) { cmpthese(-$ENV{BENCHMARK}, { cur_strlen => sub { cur_strlen($string, $position) }, rlindex => sub { rlindex($string, $position) }, op => sub { op($string, $position) }, daxim => sub { daxim($string, $position) }, }); } my @tests = ( [ "\nString\n", 2, 6, 'Surrounded' ], [ "String\n", 2, 6, 'No first \n' ], [ "\nString", 2, 6, 'No last \n' ], [ "String", 2, 6, 'No \n' ], [ "", 0, 0, 'Blank' ], ); if ($ENV{TEST}) { for (@tests) { ($string, $position, my $expected, my $desc) = @$_; is cur_strlen($string, $position), $expected, "cur_strlen: $de +sc"; is rlindex($string, $position), $expected, "rlindex: $desc" +; is op($string, $position), $expected, "op: $desc"; is daxim($string, $position), $expected, "daxim: $desc"; } done_testing; } use Inline C => q@ int cur_strlen(char * str, int pos) { int rindex = 0; int lindex = 0; for (rindex = pos; str[rindex] != '\n' && str[rindex]; rindex++); for (lindex = pos; lindex && str[lindex] != '\n'; lindex--); if (lindex == 0 && str[lindex] != '\n') lindex--; return(rindex - lindex - 1); } @; sub rlindex { my ($str, $pos) = @_; my $right = index $str, "\n", $pos; my $left = rindex $str, "\n", $pos; $right = length $str if $right == -1; $right - $left - 1; } sub daxim($s, $p, $n = "\n") { no warnings 'uninitialized'; my ($prev, $next); while ($s =~ /$n/g) { $prev = $next; $next = pos $s; last if $next > $p; } return $next - $prev; } sub tybalt89 { my ($string, $position) = @_; pos($string) = $position; $string =~ /.*\G.*\n?/; my $length_of_line = length $&; } sub op { my ($str, $pos) = @_; die "str is not defined" unless defined $str; die "pos is not defined" unless defined $pos; my @newlines; # for storing \n positions push @newlines, 0; while ($str =~/\n/g) { push @newlines, pos($str); } my $itr = scalar @newlines - 1; while ($newlines[$itr] > $pos) { $itr--; } my $length_of_line = $newlines[$itr + 1] - $newlines[$itr]; }
use strict; use warnings; omitted for brevity.

In reply to Re: Finding length of line if have any position of any char inside line by rjt
in thread Finding length of line if have any position of any char inside line by phoenix007

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 browsing the Monastery: (8)
As of 2024-04-23 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found