#Correctness
Correctness
#Correctness
Correctness
If you're either uncomfortable with Linq, or really looking to just push the performance to the absolute best it can be, then we can just modify your original algorithm to return early when we know that the answer is false.
Function IsValidString(input As String) As Boolean
For Each character In input
If Not {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".", "i"}.Contains(character) Then
Return False
End If
Next
Return True
End Function
This outperforms the both version above in every case, mostly valid or mostly invalid and it does it consistently each time I run the updated benchmarks .
Sample Size: 1000 Original; Mostly Invalid: 2ms Linq; MostlyInvalid: 0ms Improved; Mostly Invalid: 0ms Original; Mostly Valid: 2ms Linq; Mostly Valid: 2ms Improved; Mostly Valid: 1ms Sample Size: 10000 Original; Mostly Invalid: 25ms Linq; MostlyInvalid: 4ms Improved; Mostly Invalid: 3ms Original; Mostly Valid: 22ms Linq; Mostly Valid: 20ms Improved; Mostly Valid: 17ms Sample Size: 100000 Original; Mostly Invalid: 242ms Linq; MostlyInvalid: 43ms Improved; Mostly Invalid: 33ms Original; Mostly Valid: 235ms Linq; Mostly Valid: 238ms Improved; Mostly Valid: 186ms Sample Size: 1000000 Original; Mostly Invalid: 2479ms Linq; MostlyInvalid: 609ms Improved; Mostly Invalid: 336ms Original; Mostly Valid: 2358ms Linq; Mostly Valid: 2059ms Improved; Mostly Valid: 1704ms
If you're either uncomfortable with Linq, or really looking to just push the performance to the absolute best it can be, then we can just modify your original algorithm to return early when we know that the answer is false.
Function IsValidString(input As String) As Boolean
For Each character In input
If Not {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".", "i"}.Contains(character) Then
Return False
End If
Next
Return True
End Function
This outperforms the both version above in every case, mostly valid or mostly invalid and it does it consistently each time I run the updated benchmarks .
Sample Size: 1000 Original; Mostly Invalid: 2ms Linq; MostlyInvalid: 0ms Improved; Mostly Invalid: 0ms Original; Mostly Valid: 2ms Linq; Mostly Valid: 2ms Improved; Mostly Valid: 1ms Sample Size: 10000 Original; Mostly Invalid: 25ms Linq; MostlyInvalid: 4ms Improved; Mostly Invalid: 3ms Original; Mostly Valid: 22ms Linq; Mostly Valid: 20ms Improved; Mostly Valid: 17ms Sample Size: 100000 Original; Mostly Invalid: 242ms Linq; MostlyInvalid: 43ms Improved; Mostly Invalid: 33ms Original; Mostly Valid: 235ms Linq; Mostly Valid: 238ms Improved; Mostly Valid: 186ms Sample Size: 1000000 Original; Mostly Invalid: 2479ms Linq; MostlyInvalid: 609ms Improved; Mostly Invalid: 336ms Original; Mostly Valid: 2358ms Linq; Mostly Valid: 2059ms Improved; Mostly Valid: 1704ms
But does it perform? Well, actually, yes it does. I wrote some benchmarking code and it turns out that the Linq version out performs yours by quite a bit. I don't knowwas kind of surprised by this, but here are the results. I'll be back after
Sample Size: 100 Original; Mostly Invalid: 10ms Linq; MostlyInvalid: 0ms Original; Mostly Valid: 0ms Linq; Mostly Valid: 0ms Sample Size: 1000 Original; Mostly Invalid: 2ms Linq; MostlyInvalid: 1ms Original; Mostly Valid: 2ms Linq; Mostly Valid: 1ms Sample Size: 10000 Original; Mostly Invalid: 25ms Linq; MostlyInvalid: 4ms Original; Mostly Valid: 25ms Linq; Mostly Valid: 23ms Sample Size: 100000 Original; Mostly Invalid: 235ms Linq; MostlyInvalid: 41ms Original; Mostly Valid: 231ms Linq; Mostly Valid: 255ms Sample Size: 1000000 Original; Mostly Invalid: 2408ms Linq; MostlyInvalid: 587ms Original; Mostly Valid: 2347ms Linq; Mostly Valid: 2083ms Press any key to continue . . .
As you can see, the Linq version nearly always outperforms your method on mostly invalid input. However, on mostly valid input, it performs roughly the same; it's sometimes better, sometimes worse. I generate some test datasuspect this is because the Linq version bails out of the check earlier on invalid input.
Here's the final version of the benchmarking code. It exceed's .NetFiddle's memory limits, so you'll need to paste it into a C# console program to see the results of the higher sample sizes.
BUT........
#Correctness
Is 0323.233.12.i
a valid string? Both your and my method says it is. Your regex also suffers from the same problem.
But does it perform? I don't know. I'll be back after I generate some test data.
But does it perform? Well, actually, yes it does. I wrote some benchmarking code and it turns out that the Linq version out performs yours by quite a bit. I was kind of surprised by this, but here are the results.
Sample Size: 100 Original; Mostly Invalid: 10ms Linq; MostlyInvalid: 0ms Original; Mostly Valid: 0ms Linq; Mostly Valid: 0ms Sample Size: 1000 Original; Mostly Invalid: 2ms Linq; MostlyInvalid: 1ms Original; Mostly Valid: 2ms Linq; Mostly Valid: 1ms Sample Size: 10000 Original; Mostly Invalid: 25ms Linq; MostlyInvalid: 4ms Original; Mostly Valid: 25ms Linq; Mostly Valid: 23ms Sample Size: 100000 Original; Mostly Invalid: 235ms Linq; MostlyInvalid: 41ms Original; Mostly Valid: 231ms Linq; Mostly Valid: 255ms Sample Size: 1000000 Original; Mostly Invalid: 2408ms Linq; MostlyInvalid: 587ms Original; Mostly Valid: 2347ms Linq; Mostly Valid: 2083ms Press any key to continue . . .
As you can see, the Linq version nearly always outperforms your method on mostly invalid input. However, on mostly valid input, it performs roughly the same; it's sometimes better, sometimes worse. I suspect this is because the Linq version bails out of the check earlier on invalid input.
Here's the final version of the benchmarking code. It exceed's .NetFiddle's memory limits, so you'll need to paste it into a C# console program to see the results of the higher sample sizes.
BUT........
#Correctness
Is 0323.233.12.i
a valid string? Both your and my method says it is. Your regex also suffers from the same problem.