Click here to Skip to main content
15,885,852 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Count char in string? Pin
Jochen Arndt21-Aug-18 23:32
professionalJochen Arndt21-Aug-18 23:32 
GeneralRe: Count char in string? Pin
Chris Quinn22-Aug-18 4:23
Chris Quinn22-Aug-18 4:23 
GeneralRe: Count char in string? Pin
Jochen Arndt22-Aug-18 5:08
professionalJochen Arndt22-Aug-18 5:08 
GeneralRe: Count char in string? Pin
Richard Deeming22-Aug-18 8:58
mveRichard Deeming22-Aug-18 8:58 
GeneralRe: Count char in string? Pin
Chris Quinn22-Aug-18 20:55
Chris Quinn22-Aug-18 20:55 
GeneralRe: Count char in string? Pin
Jochen Arndt22-Aug-18 21:16
professionalJochen Arndt22-Aug-18 21:16 
GeneralRe: Count char in string? Pin
Chris Quinn22-Aug-18 21:40
Chris Quinn22-Aug-18 21:40 
GeneralRe: Count char in string? Pin
Richard Deeming23-Aug-18 9:03
mveRichard Deeming23-Aug-18 9:03 
Using BenchmarkDotNet[^]:
C#
public class StringCharCountBenchmark
{
    private static readonly string _theStringToSearch = "Every instance. Replace() will inteiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiirnally loop throughiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii the entire source string. It replaces all occurrences of the substring you want to replace. Tip: This behavior is useful when dealing with common string replacements in programs.  But: If you do not understand this behavior you may end up writing inefficient code that has unneeded loops. ";
    private static readonly char _theCharToFind = 'i';
    private static readonly string _theCharToFindAsString = "i";

    [Benchmark]
    public int UsingReplace()
    {
        return _theStringToSearch.Length - _theStringToSearch.Replace(_theCharToFindAsString, "").Length;
    }

    [Benchmark]
    public int UsingForEachLoop()
    {
        int result = 0;
        foreach (char c in _theStringToSearch)
        {
            if (c == _theCharToFind)
            {
                result++;
            }
        }

        return result;
    }
    
    [Benchmark]
    public int UsingForLoop()
    {
        int result = 0;
        for (int i = 0; i < _theStringToSearch.Length; i++)
        {
            if (_theStringToSearch[i] == _theCharToFind)
            {
                result++;
            }
        }

        return result;
    }
}
(Yes, I know: C#. But I'm sure you can convert to VB.NET if required. Smile | :) )

Results:
// * Summary *

BenchmarkDotNet=v0.11.1, OS=Windows 10.0.17134.228 (1803/April2018Update/Redstone4)
Intel Core i7-4770K CPU 3.50GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
Frequency=3417969 Hz, Resolution=292.5714 ns, Timer=TSC
  [Host]     : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.3132.0
  DefaultJob : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.3132.0


           Method |       Mean |     Error |    StdDev |
----------------- |-----------:|----------:|----------:|
     UsingReplace | 3,923.6 ns | 15.267 ns | 12.749 ns |
 UsingForEachLoop |   350.1 ns |  4.453 ns |  3.948 ns |
     UsingForLoop |   376.1 ns |  7.239 ns |  8.046 ns |

Both loop options are fairly close. The String.Replace version takes roughly 10x as long.

The tool doesn't measure the memory usage, but it should be fairly self-evident that the Replace version will use more memory, since it has to allocate a new string each time.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

AnswerRe: Count char in string? Pin
Chris Quinn23-Aug-18 3:19
Chris Quinn23-Aug-18 3:19 
QuestionAlternative to FileCopy in Visual Basic 6 for 2GB files up Pin
paolo71xx21-Aug-18 6:58
paolo71xx21-Aug-18 6:58 
AnswerRe: Alternative to FileCopy in Visual Basic 6 for 2GB files up Pin
Dave Kreskowiak21-Aug-18 8:09
mveDave Kreskowiak21-Aug-18 8:09 
GeneralRe: Alternative to FileCopy in Visual Basic 6 for 2GB files up Pin
paolo71xx21-Aug-18 8:28
paolo71xx21-Aug-18 8:28 
GeneralRe: Alternative to FileCopy in Visual Basic 6 for 2GB files up Pin
Dave Kreskowiak21-Aug-18 9:25
mveDave Kreskowiak21-Aug-18 9:25 
GeneralRe: Alternative to FileCopy in Visual Basic 6 for 2GB files up Pin
paolo71xx21-Aug-18 10:04
paolo71xx21-Aug-18 10:04 
GeneralRe: Alternative to FileCopy in Visual Basic 6 for 2GB files up Pin
Dave Kreskowiak21-Aug-18 10:11
mveDave Kreskowiak21-Aug-18 10:11 
GeneralRe: Alternative to FileCopy in Visual Basic 6 for 2GB files up Pin
paolo71xx23-Aug-18 2:25
paolo71xx23-Aug-18 2:25 
QuestionThe Problem in Update Command with Table Adapter VB.net Pin
Member 128130745-Aug-18 18:29
Member 128130745-Aug-18 18:29 
AnswerRe: The Problem in Update Command with Table Adapter VB.net Pin
CHill606-Aug-18 1:54
mveCHill606-Aug-18 1:54 
AnswerRe: The Problem in Update Command with Table Adapter VB.net Pin
Richard MacCutchan6-Aug-18 3:10
mveRichard MacCutchan6-Aug-18 3:10 
GeneralRe: The Problem in Update Command with Table Adapter VB.net Pin
Member 128130746-Aug-18 15:55
Member 128130746-Aug-18 15:55 
GeneralRe: The Problem in Update Command with Table Adapter VB.net Pin
Richard MacCutchan6-Aug-18 20:54
mveRichard MacCutchan6-Aug-18 20:54 
GeneralRe: The Problem in Update Command with Table Adapter VB.net Pin
Dave Kreskowiak7-Aug-18 6:14
mveDave Kreskowiak7-Aug-18 6:14 
GeneralRe: The Problem in Update Command with Table Adapter VB.net Pin
Member 128130747-Aug-18 15:37
Member 128130747-Aug-18 15:37 
GeneralRe: The Problem in Update Command with Table Adapter VB.net Pin
Member 128130747-Aug-18 22:57
Member 128130747-Aug-18 22:57 
GeneralRe: The Problem in Update Command with Table Adapter VB.net Pin
Member 128130747-Aug-18 18:02
Member 128130747-Aug-18 18:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.