Browse Source

Merge branch 'patch-7' of https://github.com/knappador/FrameworkBenchmarks into knappador-patch-7

Patrick Falls 12 years ago
parent
commit
855aa4c1e8
1 changed files with 27 additions and 6 deletions
  1. 27 6
      django/hello/world/views.py

+ 27 - 6
django/hello/world/views.py

@@ -8,6 +8,8 @@ from django.shortcuts import render
 import ujson
 import random
 from operator import attrgetter
+import numpy.random as nprnd
+from functools import partial
 
 def json(request):
   response = {
@@ -17,13 +19,32 @@ def json(request):
 
 def db(request):
   queries = int(request.GET.get('queries', 1))
-  worlds  = []
-
-  for i in range(queries):
+  # worlds = []
+  # it's not required to explicitly loop instead of using list comprehension
+  #for i in range(queries):
     # get a random row, we know the ids are between 1 and 10000
-    worlds.append(World.objects.get(id=random.randint(1, 10000)))
-
-  return HttpResponse(serializers.serialize("json", worlds), mimetype="application/json")
+    #worlds.append(World.objects.get(id=random.randint(1, 10000)))
+  # instead we can do:
+  #worlds = [World.objects.get(id=random.randint(1, 10000)) for i in range(queries)]
+  
+  # fun fact:  every dot-notation lookup calls some python magic under the hood.  Like every other code,
+  # one can eliminate dereferences by storing the end dereferenced thing in an identifier
+  g = World.objects.get
+  #r = random.randint
+  # but wait! there's more!
+  #http://stackoverflow.com/questions/4172131/create-random-list-of-integers-in-python
+  #r = nprnd.randint
+  # but wait!  there's more!  if we're calling a function over and over with the same parameters, 
+  # we can use even more function magic.
+  rp = partial(nprnd.randint, 10000)
+  # now we're ready to write our awesome query iterator thingy
+  # first of all, we know the id's correspond to the random number we're picking, so we can create
+  # dictionaries on the fly instead of serializing later
+  # by creating dicts, we don't need to user the model serializer, which is probably slow and only appropriate
+  # for complicated serializations of joins and crazy query sets etc
+  # test xrange vs range if the query number is gigantic
+  worlds = ujson.dumps([{'id' : r, 'randomNumber' : g(id=r).randomnumber} for r in [rp() for q in xrange(queries)]])  
+  return HttpResponse(worlds, mimetype="application/json")
 
 def fortunes(request):
   fortunes = list(Fortune.objects.all())