get the number of appearance of a substring in a String

Discussion in 'Java' started by Guest, Apr 12, 2005.

  1. Guest

    Guest Guest

    I there a fast way to find how many times a String contains char "\n"?

    I cannot reinvent the wheel because I want it fast, so I want something
    ready from java api


    thanx
    Guest, Apr 12, 2005
    #1
    1. Advertising

  2. Guest

    Betty Guest

    "<- Chameleon ->" <> wrote in message
    news:d3hbg0$krd$...
    > I there a fast way to find how many times a String contains char "\n"?
    >
    > I cannot reinvent the wheel because I want it fast, so I want something
    > ready from java api
    >
    >
    > thanx


    class Count {
    public static void main(String args[]) {
    String a = "what\nit\nis\n";
    int count = 0;
    System.out.println(a);
    for(int i=0; i< a.length(); i++) {
    if(a.charAt(i)=='\n')
    count++;
    }
    System.out.println(count);
    System.exit(0);
    }
    }
    Betty, Apr 12, 2005
    #2
    1. Advertising

  3. Guest

    hibiki Guest

    <- Chameleon -> a écrit :
    > I there a fast way to find how many times a String contains char "\n"?
    >
    > I cannot reinvent the wheel because I want it fast, so I want something
    > ready from java api
    >
    >
    > thanx


    A really short one :

    public/private int occur(String myString, string myPattern){
    int count = 0;

    for(int i = 0; (i = myString.indexOf(myPattern, i)) >= 0; i++)
    count += 1;

    return count;
    }

    I hope it will be helpful

    --
    Salutations,

    Joachim Naulet

    06 14 90 06 21
    http://jnaulet.no-ip.com
    hibiki, Apr 12, 2005
    #3
  4. Guest

    Guest Guest

    >>I there a fast way to find how many times a String contains char "\n"?
    >>
    >>I cannot reinvent the wheel because I want it fast, so I want something
    >>ready from java api

    >
    > class Count {
    > public static void main(String args[]) {
    > String a = "what\nit\nis\n";
    > int count = 0;
    > System.out.println(a);
    > for(int i=0; i< a.length(); i++) {
    > if(a.charAt(i)=='\n')
    > count++;
    > }
    > System.out.println(count);
    > System.exit(0);
    > }
    > }



    lol!
    thanx Betty but it is slow because it is written in Java and not in C++
    (like Java's API)

    I wonder if there is a Java API function for this...
    Until now, I use your approach...
    Guest, Apr 12, 2005
    #4
  5. <- Chameleon -> wrote:
    >>> I there a fast way to find how many times a String contains char "\n"?
    >>>
    >>> I cannot reinvent the wheel because I want it fast, so I want something
    >>> ready from java api


    ....

    > lol!
    > thanx Betty but it is slow because it is written in Java and not in C++
    > (like Java's API)


    Most of the Java API is written in Java,
    only a few native methods are declared.
    Thomas Schodt, Apr 13, 2005
    #5
  6. "<- Chameleon ->" <> writes:

    > thanx Betty but it is slow because it is written in Java and not in
    > C++ (like Java's API)


    90% of the Java libraries are written in Java. The bytecodes are most
    often translated to native at runtime, and should run as fast as C++,
    unless you take the miniscule translation time into account.
    Tor Iver Wilhelmsen, Apr 13, 2005
    #6
  7. Tor Iver Wilhelmsen wrote:
    > "<- Chameleon ->" <> writes:
    >
    >
    >> thanx Betty but it is slow because it is written in
    >> Java and not in C++ (like Java's API)

    >
    >
    > 90% of the Java libraries are written in Java. The
    > bytecodes are most often translated to native at runtime,
    > and should run as fast as C++, unless you take the
    > miniscule translation time into account.


    Incidentally, the OP should be reassured about Java
    performance. The Java libraries, which are mostly Java, run
    fast enough for the OP to assume they are in C++.

    Patricia
    Patricia Shanahan, Apr 13, 2005
    #7
  8. Guest

    Guest Guest

    >>>> I there a fast way to find how many times a String contains char
    "\n"?
    >>>>
    >>>> I cannot reinvent the wheel because I want it fast, so I want something
    >>>> ready from java api

    >
    >
    > ....
    >
    >> lol!
    >> thanx Betty but it is slow because it is written in Java and not in
    >> C++ (like Java's API)

    >
    >
    > Most of the Java API is written in Java,
    > only a few native methods are declared.


    Whooow! in Java?!
    Something new to learn ;-)
    Thanks!
    Guest, Apr 13, 2005
    #8
  9. Guest

    bugbear Guest

    <- Chameleon -> wrote:
    >>> I there a fast way to find how many times a String contains char "\n"?
    >>>
    >>> I cannot reinvent the wheel because I want it fast, so I want something
    >>> ready from java api


    Other people have noted that the libraries are themselves coded in java
    (unlike perl, typically).

    You might try this, although I doubt it will be any faster.

    int count = 0;
    for(int pos = 0; (pos = a.indexOf('\n', pos)) > 0; pos++) {
    count++;
    }

    caveat; I have neither compiled nor tested this.

    BugBear
    bugbear, Apr 13, 2005
    #9
  10. Guest

    HomerCritic Guest

    <- Chameleon -> wrote:
    > I there a fast way to find how many times a String contains char

    "\n"?

    I did a quick performance comparison:


    =========== test.cpp ============
    #include <iostream>

    using namespace std;

    int count(char *pString)
    {
    int total = 0;
    for (int c; (c = *pString++); )
    if (c == '\n')
    total++;
    return total;
    }

    int main()
    {
    char *pString =
    "test\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\n";
    int n;
    for (int i = 0; i < 10000000; i++)
    n = count(pString);
    cout << n << endl;
    return 0;
    }


    ============= Test.java ===============

    public class Test
    {
    public static int count(String s)
    {
    int total = 0;
    /*
    char[] chars = s.toCharArray();
    for (int i = chars.length; --i >= 0; )
    if (chars == '\n')
    total++;
    */
    for (int i = s.length(); --i >= 0; )
    if (s.charAt(i) == '\n')
    total++;
    return total;
    }
    public static int count(char[] chars)
    {
    int total = 0;
    for (int i = chars.length; --i >= 0; )
    if (chars == '\n')
    total++;
    return total;
    }
    public static void main(String[] args) throws Exception
    {
    long time = System.currentTimeMillis();
    String string =
    "test\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\n";

    int n = 0;
    for (int i = 0; i < 10000000; i++)
    n = count(string);
    System.out.println(n);
    System.out.println(System.currentTimeMillis() - time);

    time = System.currentTimeMillis();
    char[] chars = string.toCharArray();
    n = 0;
    for (int i = 0; i < 10000000; i++)
    n = count(chars);
    System.out.println(n);
    System.out.println(System.currentTimeMillis() - time);

    System.exit(0);
    }
    }

    ================================================================================

    $ cc -O3 -o test -lstdc++ test.cpp
    $ time ./test
    18
    1.92user 0.01system 0:01.95elapsed 98%CPU (0avgtext+0avgdata
    0maxresident)k
    0inputs+0outputs (0major+223minor)pagefaults 0swaps

    =================================================================================

    $ javac Test.java
    $ java -server Test
    18
    4438
    18
    3331

    ==================================================================================

    note: the -server flag tells Java to expect a server environment, where
    there is a preference to compile bytecodes into machine language sooner
    rather than later.

    Ok, 1.95 seconds in C vs 3.3 seconds in Java.
    This is a very loose comparison ofcourse, but one could (ignorantly)
    conclude that Java must be roughly 50% slower than C. This is much
    less bad than many C die-hards and/or anti Java people think by the
    way.

    Java has this delayed bytecode to machine code translation engine.
    Without the -server flag, I'm getting about a 5 second time. This is
    because the Java VM is waiting for the CPU to become idle (nothing to
    do), which it will use to improve bytecodes into machine code.

    In the end, ultimately, logic for logic, Java is most of the time a
    little slower, sometimes faster. But not nearly as astronomically much
    slower as many stuborn C diehards continue to think. With Java around,
    companies would be nuts to invest in C/C++ development these days,
    unless there is a performance case for it.

    You can supplement Java with C/C++ and Assembler even, for that oddball
    compression routine, or graphic processing routines and such.

    In C#, it's probably the same story, but I'm a Java diehard, and
    resistance is *NOT* futile.


    - Homer -
    HomerCritic, Apr 13, 2005
    #10
  11. Guest

    Guest

    Why not compare a Java character array vs. C
    style strings, and then Java String objects agains
    C++ string objects? I don't know what the results
    will be, but this seems like a more level playing
    field.
    , Apr 14, 2005
    #11
  12. HomerCritic wrote:
    > Ok, 1.95 seconds in C vs 3.3 seconds in Java.
    > This is a very loose comparison ofcourse, but one could (ignorantly)
    > conclude that Java must be roughly 50% slower than C. This is much
    > less bad than many C die-hards and/or anti Java people think by the
    > way.


    In your example you are comparing access via a pointer with access via
    an index. There is a good chance that the pointer access and increment
    can be compiled into very few assembler statements. If you have a
    processor which has an assembler instruction for incrementing a
    register, not much is needed for the pointer arithmetic, and a good C
    compiler can reduce the loop body to almost nothing.

    Compared to the index access in Java, where - in the non optimized case
    - you need some real arithmetic to calculate the address of the element,
    and where you probably have got to go through an additional indirection
    level just to get the base of the array (dereference the object), the
    Java result is almost to good :)

    > In the end, ultimately, logic for logic, Java is most of the time a
    > little slower, sometimes faster. But not nearly as astronomically much
    > slower as many stuborn C diehards continue to think. With Java around,
    > companies would be nuts to invest in C/C++ development these days,
    > unless there is a performance case for it.


    This reminds me of a "benchmark" in an otherwise good German computer
    magazine. One of their regular writers (a die-hard Delphi and
    <whatever>.NET programmer) was running a home-grown toy "benchmark". It
    was a collection of meaningless small programs. I had the impression
    that he tried very hard to let his favorite languages look good, but at
    the end he had to admit that the Java results weren't to bad. His C/C++
    stuff was in general slower, only his beloved Delphi stuff was most of
    the times faster - surprise, surprise :)

    > You can supplement Java with C/C++ and Assembler even, for that oddball
    > compression routine, or graphic processing routines and such.


    You don't need to do that for graphics, unless you need the last bit of
    performance. More and more of the Java2D graphics system use the
    underlying OS graphics system via native methods to do things.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    Thomas Weidenfeller, Apr 14, 2005
    #12
  13. Guest

    bugbear Guest

    Thomas Weidenfeller wrote:
    > HomerCritic wrote:
    >
    >> Ok, 1.95 seconds in C vs 3.3 seconds in Java.
    >> This is a very loose comparison ofcourse, but one could (ignorantly)
    >> conclude that Java must be roughly 50% slower than C. This is much
    >> less bad than many C die-hards and/or anti Java people think by the
    >> way.

    >
    >
    > In your example you are comparing access via a pointer with access via
    > an index. There is a good chance that the pointer access and increment
    > can be compiled into very few assembler statements. If you have a
    > processor which has an assembler instruction for incrementing a
    > register, not much is needed for the pointer arithmetic, and a good C
    > compiler can reduce the loop body to almost nothing.
    >
    > Compared to the index access in Java, where - in the non optimized case
    > - you need some real arithmetic to calculate the address of the element,
    > and where you probably have got to go through an additional indirection
    > level just to get the base of the array (dereference the object), the
    > Java result is almost to good :)



    Heh. You underestimate compiler technology. I strongly
    suspect that common idioms e.g. dereferences of successive
    indices are spotted and optimised.

    This page refers to optimising C, but the concepts
    generalise nicely.
    http://www.nullstone.com/htmls/category.htm

    BugBear
    bugbear, Apr 14, 2005
    #13
  14. Guest

    Robert Guest

    I wonder if String.split( '\n" ); might not be fast enough. If not I'd
    get the char[] from the string and loop through it.
    Robert, Apr 14, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nico Grubert
    Replies:
    1
    Views:
    433
    Pierre Quentel
    Jun 21, 2006
  2. Replies:
    4
    Views:
    750
    Malcolm
    Oct 2, 2005
  3. Replies:
    3
    Views:
    187
    Sherm Pendley
    Aug 3, 2005
  4. PerlFAQ Server
    Replies:
    0
    Views:
    159
    PerlFAQ Server
    Jan 4, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    148
    PerlFAQ Server
    Apr 22, 2011
Loading...

Share This Page