July 19, 2016 Re: Atila Neves: "C IS NOT MAGICALLY FAST, PART 2" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 07/19/2016 12:00 PM, Ali Çehreli wrote: > On 07/19/2016 10:41 AM, deadalnix wrote: >> if(i < other.i) return -1; >> if(i > other.i) return 1; >> >> Should be >> >> (i > other.i) - (i < other.i) >> >> Surprisingly, LLVM was unable to optimize one into the other in my tests. If you mean the following, it's slower with ldc: const r = (i > other.i) - (i < other.i) ; if (r) return r; > Additionally, the string may be traversed twice in opCmp. The following > change makes D example faster: > > import std.algorithm : cmp; > return cmp(s, other.s); > // return s < other.s > // ? -1 > // : (s > other.s ? 1 : 0); Faster with dmd but has no difference with ldc. Ali |
July 19, 2016 Re: Atila Neves: "C IS NOT MAGICALLY FAST, PART 2" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | On Tuesday, 19 July 2016 at 16:13:21 UTC, Atila Neves wrote: > On Tuesday, 19 July 2016 at 16:01:01 UTC, Lodovico Giaretta wrote: >> On Tuesday, 19 July 2016 at 15:48:26 UTC, Atila Neves wrote: >>> Small string optimization should _help_ std::string, no? >>> >>> Atila >> >> Small string optimization will make the struct bigger, thus making swapping slower. If the struct is no bigger than 2 pointers, swapping it is ultra fast. > > Interesting. Read up on it and tried changing the strings to be ~50 chars long. C++/std::string gets better, but C++/const char* still beats D by a small margin: > > C: 1.852s > C++/std::string: 1.489s > C++/const char*: 1.034s > D: 1.188s > > Could be the cost of swapping the "fat" in "fat pointer", in which case: worth it. You are swapping different amount of data. For C++/const char* vs D, you are swapping 75% more data. For D vs C++/std::string, you are swsapping 129% more data. I would expect you getting the same numbers if you did "const(char)*" version in D? And if you used a inline sort[1] in the C version it would probably be just as fast as the C++/const char* version. Also it was a bit hard to find out what N you where using. Cheers, Jakob. [1] http://www.corpit.ru/mjt/qsort.html |
Copyright © 1999-2021 by the D Language Foundation