Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

sort

Name: Anonymous 2010-02-15 17:09

sort() can be compared to the qsort() function in the C standard library (in stdlib.h). sort is templated, so it uses the correct comparison function for whatever data type you are sorting, which is already implemented for standard data types. Otherwise you can specify your own comparison function, which can be type-checked by the compiler; whereas for qsort, you must manually cast pointers to the desired type in an unsafe way. Also, qsort accesses the comparison function using a function pointer, necessitating large numbers of repeated function calls, which can take a lot of time; whereas in C++, comparison functions are usually inlined, removing the need for function calls. In practice, C++ code using sort is often many times faster at sorting simple data like integers than equivalent C code using qsort.

Name: Anonymous 2010-02-16 7:38

sort in CL is quite nice: it can sort a sequence( a list, some kind of vector whose internal representation can vary (such as boxed, unboxed, displaced, depending on what your implementation supports), or even a custom sequence (again, if implementation supports)) using a user-given predicate(either your own, or builtins like >, <, string> and so on), it can also apply a keying function to the array, which allows you to filter elements individually in some way without actually modifying them (result is used when comparing). It's very easy to use(like: (sort sequence #'<)), picks a better algorithm depending the underlying sequence (qsort is good for arrays, but not so smart for linked lists, since random access costs O(n), not O(1)). Obviously, one can always implement a superior sort if they so wish, but this is pretty good. And whoever mentioned inlining being a problem: it's pretty easy to do in CL, just declare the function as inline, and you can also do local declarations if you want a declaration to not apply or apply to a specific block of code.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List