Click here to Skip to main content
15,905,028 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Could someone explain why For Each and For Next loops behave differently with lists? Pin
Eddy Vluggen9-Sep-18 2:11
professionalEddy Vluggen9-Sep-18 2:11 
GeneralRe: Could someone explain why For Each and For Next loops behave differently with lists? Pin
MattiasW7620-Sep-18 9:09
MattiasW7620-Sep-18 9:09 
AnswerRe: Could someone explain why For Each and For Next loops behave differently with lists? Pin
Dave Kreskowiak9-Sep-18 4:54
mveDave Kreskowiak9-Sep-18 4:54 
GeneralRe: Could someone explain why For Each and For Next loops behave differently with lists? Pin
MattiasW7627-Sep-18 2:59
MattiasW7627-Sep-18 2:59 
QuestionAdding Custom TabPage to TabControl: Closed Pin
mo14927-Sep-18 11:12
mo14927-Sep-18 11:12 
QuestionHow to declare Crypto? Pin
Member 139535037-Sep-18 6:04
Member 139535037-Sep-18 6:04 
AnswerRe: How to declare Crypto? Pin
Richard Deeming7-Sep-18 7:46
mveRichard Deeming7-Sep-18 7:46 
GeneralRe: How to declare Crypto? Pin
Member 1395350314-Sep-18 3:19
Member 1395350314-Sep-18 3:19 
QuestionCurrency textbox Pin
Member 139744835-Sep-18 16:08
Member 139744835-Sep-18 16:08 
AnswerRe: Currency textbox Pin
Mycroft Holmes5-Sep-18 18:34
professionalMycroft Holmes5-Sep-18 18:34 
AnswerRe: Currency textbox Pin
dan!sh 5-Sep-18 19:56
professional dan!sh 5-Sep-18 19:56 
Questionwindows version in vb.net Pin
JR2123-Sep-18 0:20
JR2123-Sep-18 0:20 
AnswerRe: windows version in vb.net Pin
Eddy Vluggen3-Sep-18 1:45
professionalEddy Vluggen3-Sep-18 1:45 
AnswerRe: windows version in vb.net Pin
Dave Kreskowiak3-Sep-18 13:31
mveDave Kreskowiak3-Sep-18 13:31 
QuestionHow to multi Highlight text different 2 textbox in datagridview VB.net? Pin
CodieCalm2-Sep-18 22:26
CodieCalm2-Sep-18 22:26 
QuestionCount char in string? Pin
Member 1395409621-Aug-18 22:43
Member 1395409621-Aug-18 22:43 
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 

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.