Domanda

I have a Django model with score and quizzes_done. I want to calculate percentiles on both scores and quizzes_done. To do that, I need to create two sorted lists. For the first one, I do:

s_recs = MyRecord.objects.all().order_by('score')
score_list = [r.score for r in s_recs]

I can do the same to get a sorted quiz_list or I can use python sort. I am wondering which one is faster.

Use the s_recs we got above and do

quizzes_list = [r.quizzes_done for r in s_recs]
quizzes_list.sort()

OR

q_recs = MyRecord.objects.all().order_by('quizzes_done')
quizzes_list = [r.quizzes_done for r in q_recs]
È stato utile?

Soluzione

Here is a little function that you can pass them both through to time:

def timedcall(fn, *args):
    "Call function with args; return the time in seconds and result."
    t0 = time.clock()
    result = fn(*args)
    t1 = time.clock()
    return t1-t0, result

This returns the timing and any result returned by the function you call. I think you will find that unless you are sorting upwards of 10s of thousands of records there will be little difference, but I would be interested to hear the results.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top