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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.