Re: Testing slightly different python randomisation maneuvers

  • From: "Jacob Kruger" <jacobk@xxxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 5 May 2011 21:12:16 +0200

Basically, using the specific random module/class random's randint method/function to generate what's already a random integer value in the range from 1 to 100 - random.randint(1, 100) - when called 100 thousand times, took roundabout 4 times longer than calling the more generic/basic random object/class's standard random() method to generate a more generic random number ranging from 0 to 1, and then just turning that into a number ranging from 1 to 100 - since I'm pretty sure that's the basis for the other one, and using bare minimum/basic other bits of functionality still let it work way more efficiently than calling the sort of wrapper that's provided in this version of python I am running here - 3.2.


The total bit of code/script looked like the following:

#---start---
import time
import random
simpleStartTime = time.time()
simpleRandom = 0
for X in range(1, 100000):
  simpleRandom = random.randint(1, 100)

simpleEndTime = time.time()
fancyStartTime = time.time()
fancyRandom = 0
for X in range(1, 100000):
fancyRandom = int(random.random() * 100)

fancyEndTime = time.time()
print("the simple method took approximately %s milliseconds" % (int((simpleEndTime - simpleStartTime) * 1000))) print("the fancy method took approximately %s milliseconds" % (int((fancyEndTime - fancyStartTime) * 1000))) diffVal = int(((simpleEndTime - simpleStartTime) - (fancyEndTime - fancyStartTime)) * 1000)
print("the difference was %s milliseconds" % (diffVal))
#---end---

time.time() basically handles it like most other languages/platforms as in it returns the number of time periods/milliseconds since something like january 1, 1970 or thereabouts, and this whole bit of testing was me just wondering how efficient something like them providing a sort of wrapper function would actually help - don't think it would make any difference if you weren't literally doing it a whole bunch of times in one go, but anyway - on principle it's better to keep more efficient even if the code looks slightly less obvious/simple, if you get my drift...<smile>

Hope that makes more sense?

Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'

----- Original Message ----- From: "Homme, James" <james.homme@xxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, May 05, 2011 7:38 PM
Subject: RE: Testing slightly different python randomisation maneuvers


Hi Jacob,
I'm sorry. I don't get this. Can you please explain in smaller steps?

Thanks.

Jim

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jacob Kruger
Sent: Thursday, May 05, 2011 12:49 PM
To: programmingblind@xxxxxxxxxxxxx
Cc: program-l@xxxxxxxxxxxxx
Subject: Re: Testing slightly different python randomisation manoeuvres

Oh yes, and it literally comes down to either using the simpler version:
random.randint(1, 100)

Or the more manual version:
int(random.random() * 100)

Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'

----- Original Message -----
From: "Jacob Kruger" <jacobk@xxxxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Cc: <program-l@xxxxxxxxxxxxx>
Sent: Thursday, May 05, 2011 4:19 PM
Subject: Testing slightly different python randomisation manoeuvres


Testing python randomising a number between 1 and 100, repeating process 100
thousand times, the automatic integer output method took approximately 279
milliseconds the slightly more involved (in terms of having to convert
randomised number into a clean integer in the right range) method took
approximately 59 milliseconds - the difference was 220 milliseconds slower
if you asked it to automatically give you a number in the range 1 to 100, as
opposed to taking the decimal/floating point number more automatically
generated, and multiplying it then by 100, and converting it to an integer
as such.

This is something that almost immediately popped up in my mind when was
reviewing the various randomisation objects/methods provided by modules
built into python, after had already made my own workaround for spitting out
a clean integer between 1 and 100, and then thought of double checking if
there was some or other built in/automatic process to generate one as such,
and then I put together the little bit of code to test the differences in
execution time...<smile>

Dunno why I thought of trying this out, since am actually just busy going
through one of the learn python books, and while working through pieces on
lists and dictionary objects, I was messing around with storing the full
alphabet in these collection objects, and then spitting out a random letter,
etc. etc., and it just ended up making me get around to thinking
about/considering this.

Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


This e-mail and any attachments to it are confidential and are intended solely for use of the individual or entity to whom they are addressed. If you have received this e-mail in error, please notify the sender immediately and then delete it. If you are not the intended recipient, you must not keep, use, disclose, copy or distribute this e-mail without the author's prior permission. The views expressed in this e-mail message do not necessarily represent the views of Highmark Inc., its subsidiaries, or affiliates.
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: