- if (!ret.IsEmpty() && ret[0] != _T('"') && ret[0] != _T('\''))
+ if (str.IsEmpty() || str.StartsWith(_T("\"")) || str.StartsWith(_T("\'")))
Why is this change needed? Why have you replaced the operator[] with startswith?
I've done some benchmarking and profiling.
orig - current version in codeblocks
forum - your version + a fix when find returns npos
mine - obviously this is my implementation
The results printed by google's benchmark lib:
> bin/release/wx_routine_bench
Success: 13 tests passed.
Test time: 0.00 seconds.
Run on (4 X 3599.88 MHz CPU s)
2016-02-06 17:15:06
Benchmark Time(ns) CPU(ns) Iterations
-----------------------------------------------------------
BM_orig/1 742 741 958904
BM_orig/8 7456 7457 93333
BM_orig/64 85563 85504 8140
BM_orig/512 2997799 3000000 233
BM_orig/4k 173072815 173000000 4
BM_orig/32k 28505832434 28504000000 1
BM_orig/64k 139696512699 139686000000 1
BM_forum/1 774 774 886076
BM_forum/8 5031 5029 140000
BM_forum/64 33001 32993 21277
BM_forum/512 256982 256831 2745
BM_forum/4k 2011854 2014368 348
BM_forum/32k 19614743 19611111 36
BM_forum/64k 43754816 43764706 17
BM_mine/1 246 246 2834008
BM_mine/8 1321 1320 530303
BM_mine/64 9476 9481 74468
BM_mine/512 69313 69295 10145
BM_mine/4k 537183 536715 1321
BM_mine/32k 6133614 6135135 111
BM_mine/64k 12912220 12907407 54
BM_mine_vector/1 217 217 3225806
BM_mine_vector/8 1447 1445 479452
BM_mine_vector/64 9060 9064 77778
BM_mine_vector/512 68974 68972 10294
BM_mine_vector/4k 539424 538986 1321
BM_mine_vector/32k 6113907 6115044 113
BM_mine_vector/64k 13649380 13666667 51
BM_no_trim_orig/1 383 383 1861702
BM_no_trim_orig/8 4165 4166 168269
BM_no_trim_orig/64 61783 61825 11290
BM_no_trim_orig/512 2833573 2829960 247
BM_no_trim_orig/4k 167948902 168000000 4
BM_no_trim_orig/32k 29410523891 29410000000 1
BM_no_trim_orig/64k 137438740492 137432000000 1
BM_no_trim_forum/1 357 357 1949861
BM_no_trim_forum/8 1751 1753 400000
BM_no_trim_forum/64 11392 11384 61404
BM_no_trim_forum/512 85726 85749 8140
BM_no_trim_forum/4k 676759 676385 1029
BM_no_trim_forum/32k 8598947 8597561 82
BM_no_trim_forum/64k 19704975 19714286 35
BM_no_trim_mine/1 250 250 2788845
BM_no_trim_mine/8 1527 1528 457516
BM_no_trim_mine/64 10476 10470 66667
BM_no_trim_mine/512 78441 78449 8974
BM_no_trim_mine/4k 606494 606684 1167
BM_no_trim_mine/32k 7209706 7218750 96
BM_no_trim_mine/64k 15034847 15021739 46
BM_no_trim_mine_vector/1 224 224 3139013
BM_no_trim_mine_vector/8 1649 1649 419162
BM_no_trim_mine_vector/64 9681 9672 72165
BM_no_trim_mine_vector/512 74088 74147 9589
BM_no_trim_mine_vector/4k 585973 585750 1207
BM_no_trim_mine_vector/32k 7304332 7305263 95
BM_no_trim_mine_vector/64k 15878234 15883721 43
See the attached file for the source code.
Notes:
1. There are cases where the original version is faster than the forum version.
2. Mine version is always faster.
3. When doing trimming it is 3x or so faster. :)
4. Obviously faster code is uglier :)
@obfuscated
made another attempt on GetArrayFromString(). if you don't mind, could you give it a run in your testing harness to see its performance?
wxArrayString GetArrayFromString_3(const wxString& text, const wxString& separator, bool trimSpaces)
{
if ( text.empty() )
return wxArrayString();
const size_t seplen = separator.length();
size_t start_pos = 0;
size_t sep_pos = 0;
wxArrayString out;
if (trimSpaces)
{
while (sep_pos != wxString::npos)
{
start_pos = text.find_first_not_of(_T(" \t\n\r"), start_pos);
if (start_pos == wxString::npos)
break;
sep_pos = text.find(separator, start_pos);
if (sep_pos != start_pos)
{
const size_t end_pos = text.find_last_not_of(_T(" \t\n\r"), (sep_pos == wxString::npos) ? wxString::npos : sep_pos - 1);
out.push_back(text.substr(start_pos, end_pos - start_pos + 1));
}
start_pos = sep_pos + seplen;
}
}
else
{
while (sep_pos != wxString::npos)
{
sep_pos = text.find(separator, start_pos);
if ( sep_pos != start_pos )
out.push_back(text.substr(start_pos, (sep_pos == wxString::npos) ? wxString::npos : sep_pos - start_pos));
start_pos = sep_pos + seplen;
if (start_pos >= text.length())
break;
}
}
return out;
}