get the number of appearance of a substring in a String

G

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
 
B

Betty

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);
}
}
 
H

hibiki

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
 
G

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...
 
T

Thomas Schodt

....

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.
 
T

Tor Iver Wilhelmsen

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.
 
P

Patricia Shanahan

Tor said:
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
 
G

Guest

I there a fast way to find how many times a String contains char
"\n"?
Most of the Java API is written in Java,
only a few native methods are declared.

Whooow! in Java?!
Something new to learn ;-)
Thanks!
 
B

bugbear

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
 
H

HomerCritic

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 -
 
G

googmeister

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.
 
T

Thomas Weidenfeller

HomerCritic said:
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
 
B

bugbear

Thomas said:
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
 
R

Robert

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top