Nine11 (02-13-2017)
This is a continuation of the following thread:
https://www.mpgh.net/forum/showthread.php?t=1122156
which I can't answer because the thread is closed. Why to open a discussions about this? In programming, people do not stop learning. Since I am interested to the argument, why to not talk about it.
@Biesi closed the topic after his answer, without letting me reply.
If I was able to reply, the @OP probably had the possibility to use a valid method to do it. This was the important thing.A pathetic stratagem
I would like to quote his answer:
In C#, for 'large arrays' we would mean this:telling people to create an unnecessarily large array of strings is just bullshit
with a large buffer. Am I using them in the methods I suggested?Code:char[] foo = new char[1024];
and:Code:public static int getWordsCount(string sentence) { System.Text.RegularExpressions.MatchCollection matchCollection = System.Text.RegularExpressions.Regex.Matches(sentence, @"[\S]+"); return matchCollection.Count; }
you are also specifying I'm using an array of strings. Where? There is no array of strings, there is just a static array of char in the second method (which has just 3 elements - so large) which I passed to the Split() function like delimiter.Code:sentence.Split(new char[] {'\r', '\n', ' '}, StringSplitOptions.RemoveEmptyEntries).Length;
An array of char could actually mean just a string, not an array of strings.
Also you should concern code, you are just counting the number of spaces in a string, not the words.
Console.WriteLine(CountWords("This is a string"));
Output will be 3 (spaces). And spaces can be simply counted doing the following:
which is conceptually the same thing.Code:string s = "This is a string"; Console.WriteLine(s.Count(Char.IsWhiteSpace));
But let's do some benchmarks. These are my results in my machine:
without any modification of your and my code. But you were anyway off-topic as @OP wrote he wanted to count number of words, not of spaces in a string.Code:COUNTING SPACES IN A STRING CountWords() method (which actually counts spaces in a string) work (Biesi method) Number of spaces: 5 Completed successfully.Time elapsed: 0,0017 ms String.Count(Char.IsWhiteSpace) method work (javalover method) Number of spaces: 5 Completed successfully.Time elapsed: 0,0003 ms
My suggestion of counting words using Split() method can be simplified yet. Here a simple way of doing it without literally using any array of char:
benchmark result:Code:Console.WriteLine("this is my string hello mpgh".Split().Length);
and finally, using regex - benchmark result:Code:Number of words: 6 Completed successfully.Time elapsed: 0 ms
Summary:Code:Number of words: 6 Completed successfully.Time elapsed: 0,0003 ms
don't you believe me? Test yourself without changing any part of the codes and using the Stopwatch class.
n.b: Anyone can post here about his opinion, but flaming is not accepted.
Nine11 (02-13-2017)
I'm not a minion, so I did not close the thread (and no, former staff does not mean staff).
string.Split is creating an array of strings which you access the Length property to get the size of it. The array will be generated disregarding the fact that you're not using it as a variable. How about you try your glorious code on a text with a few 100.000 words and then attach a debugger to it
I said one way would be to count the spaces followed by at least one letter, of course this could be improved. And no, string.Count is not the same as it would count multiple spaces.
You were the one blaming me and saying the code I provided was "wrong" just because I did not return... but then providing bad practice examples
So please stop whining
Last edited by Biesi; 06-07-2016 at 05:18 AM.
Are you sure? Let's watch the decompiled code:string.Split is creating an array of strings which you access the Length property to get the size of it
what can you see now? String.Split() function internally creates an array of integers, and assignes the size on it with length of the string. The array of string is what it does return.
Then, basing to the enumeration of StringSplitOptions, it calls InternalSplitOmitEmptyEntries() or InternalSplitKeepEmptyEntries().
you were letting people understand I said I had to create large array of strings explicitly, and so externally from Split() function, while it's actually implicitly returned from the Split() function.telling people to create an unnecessarily large array of strings is just bullshit
I suggest you being more specific.
The array will be generated disregarding the fact that you're not using it as a variableTo do what? Of course, to count spaces, which is always off-topic. The spaces should be counted in a string also if they aren't followed from a letter, that's also why it's wrong.I said one way would be to count the spaces followed by at least one letter
Of course.How about you try your glorious code on a text with a few 100.000 words
So, we can confirm my way is more performant.
Imagine a string with a length of 10^40. The difference is that my method could be overkill and slower, your one would be the slowest one and obviously would not stop finishing.
The user didn't say anything about counting the words in a string with an extralarge length. I don't think he has to count words in a string with 100.000 of length.
so, assuming your affermation, in this string:And no, string.Count is not the same as it would count multiple spaces
your code would count not more than 1 space?Code:my own string
Or perhaps didn't you read what your interlocutor wrote?...
your method's output: 3.Code:string hello = "This is a string"; Console.WriteLine(CountWords(s)); // your method Console.WriteLine(s.Count(Char.IsWhiteSpace)); // mine
my method's output: 3.
The output will be the same with the small-and-wrong difference (of your code) that you are checking if the space is anyway followed by a letter.
In the context of counting words you will need to fix a little thing, and so if you want to let your code returning the number of words, you should do:
and not:Code:return count + 1;
Code:return count;your code was just ot other than being literally wrong.You were the one blaming me and saying the code I provided was "wrong" just because I did not return
So please stop whiningn.b: Anyone can post here about his opinion, but flaming is not accepted.
Nine11 (02-13-2017)
This blowed Virtual Machines like Java or .NET consumes my Memory very much and then there are Developer like Javalover who is consuming more of my precious Memory for saving some CPU Time ^^
Javalover's Method is faster, but you consume a lot of Memory with large Strings and Biesi's Method declare just 1 integer to clean up by the Garbage Collector...
Lol. It isn't my fault if you guys don't read what I write. Anyway, I've anticipated you as I've already explained this in my precedent post:
more precisely:
What does overkill mean?
Complete definition:
or simply:the amount by which destruction or the capacity for destruction exceeds what is necessary
now fit this definition to this context. Yes, we mean that.complete destruction
However I think the OP of that thread would have said if he had to count words in a large string with a length like 100.000 as it isn't often requested, but who knows.
Nine11 (02-13-2017)