I was thinking something like @shirleyquirk... and indeed it runs much faster.
I don't post absolute values because I'have slower PC, older Nim version, but
in relative the time reduction is evident. At the end I guess it's like
providing a lookup table to compiler. Modifying a little @sky_khan version:
type
Result = object
letters : int
numbers : int
proc analyze(x: string): Result =
for c in x:
case c:
of 'A'..'z':
result.letters.inc
of '0'..'9':
result.numbers.inc
else:
discard
proc main() =
const input = "uyguhijkmnbdv44354gasuygiuiolknchyqudsayd12635uha"
const res = analyze(input)
var total = Result()
for _ in 0 ..< 1000000:
total.letters += res.letters
total.numbers += res.numbers
echo "Numbers: ", total.numbers, " Letters: ", total.letters
main()
Run
- Please help me understand the performance difference between ... blashyrk92
- Please help me understand the performance difference bet... sky_khan
- Please help me understand the performance difference... blashyrk92
- Please help me understand the performance differ... sky_khan
- Please help me understand the performance di... shirleyquirk
- Please help me understand the performan... blashyrk92
- Please help me understand the perfo... tcheran
- Please help me understand the p... blashyrk92
- Please help me understand the p... mardiyah
- Please help me understand the p... treeform
- Please help me understand the p... sls1005
- Please help me understand the p... r3c
