Search code examples
pythonperformanceredditpraw

PRAW get_redditor() slow?


Has anyone else noticed that get_redditor() method of praw is a bit slow? I started working with it yesterday and when I started noticing the speed drops I decided to do a bit of a test. I use the following code :

import praw
import time

time1 = 0
time2 = 0

print('Initial reddit setup')
time1 = int(round(time.time() * 1000))
user_agent = ('Reddit Checker 0.1 by /u/NinjaXI')
r = praw.Reddit(user_agent)
time2 = int(round(time.time() * 1000))
print(time1)
print(time2)
print(time2 - time1)
print('===================================================================')

print('Get Subreddit Python')
time1 = int(round(time.time() * 1000))
pythonSub = r.get_subreddit('Python')
time2 = int(round(time.time() * 1000))
print(time1)
print(time2)
print(time2 - time1)
print('===================================================================')

print('Get hot 10')
time1 = int(round(time.time() * 1000))
pythonSub.get_hot(limit=10)
time2 = int(round(time.time() * 1000))
print(time1)
print(time2)
print(time2 - time1)
print('===================================================================')

print('Get Redditor NinjaXI')
time1 = int(round(time.time() * 1000))
me = r.get_redditor('NinjaXI')
time2 = int(round(time.time() * 1000))
print(time1)
print(time2)
print(time2 - time1)
print('===================================================================')

print('Get submitted 10')
time1 = int(round(time.time() * 1000))
me.get_submitted(limit=10)
time2 = int(round(time.time() * 1000))
print(time1)
print(time2)
print(time2 - time1)
print('===================================================================')

This outputs :

Initial reddit setup
1421319134782
1421319134783
1
===================================================================
Get Subreddit Python
1421319134783
1421319134783
0
===================================================================
Get hot 10
1421319134783
1421319134783
0
===================================================================
Get Redditor NinjaXI
1421319134783
1421319137402
2619
===================================================================
Get submitted 10
1421319137402
1421319137402
0
===================================================================

As you can see to use get_redditor() takes(in this run) 2.6 seconds longer than get_subreddit(). On average the time is between 2 and 2.6 seconds everytime, but get_subreddit() only goes as high as 1ms. Is there something I'm doing? Is it a known issue with praw/reddit api? Is there anything I can do to speed it up?


Solution

  • PRAW's getters typically return lazy objects, meaning that they only request data from Reddit's servers when you actually try to use the object. This is consistent with your very fast function calls -- everything is happening locally. Your get_redditor() call is probably requesting the data immediately (though I couldn't say why; according to the doc, it should be lazy as well).