Skip to content

Instantly share code, notes, and snippets.

View qhuang872's full-sized avatar

Qiwei Huang qhuang872

  • San Jose
View GitHub Profile
@qhuang872
qhuang872 / answer.txt
Created December 31, 2016 10:32
Qiwei's answer to optimization take-home-test
1, average runtime of the program is 52 seconds.
2, I separate the program into 4 different small functions, each serving only one purposes: read_content, get_link, download_video and download_image. So that I can measure memory usage of each part, using memory_profiler. According to the output of memory_profiler, read_content takes up the most memory: about 48 MiB. Other functions and their memory usage: get_link uses 40 MiB, download_video uses 42 MiB, and download_image uses 45 MiB.
3, in this program, variable contents which is a json object occupies most memory.
4, 1) the first way to optimize this script is to skip the "get_link" process, which means instead of storing all mp4_links and png_links into lists, since the "get_link" process is just a for loop, by eliminating this process, we are reducing runtime as well. We can just iterate through every json object, and just download the link from there. However, this solution only reduces runtime by 1~2 seconds in average, in my opinion, this is not
import os
import urllib.request
from urllib.parse import urlparse
def download_file(url, cachedir=None, ignorecache=False, filename=None) -> str:
"""download_file would create a directory if not exist, then download the file to directory"""
if not cachedir:
cachedir = os.path.join(os.getcwd(), "build")
Original error happens on line 11, that exit_ok was default to be False which caused the file_exist erorr, I made the change to set it to be True.
Also added a docstring for the download_file function.
Original error happens on line 11, that exit_ok was default to be False which caused the file_exist erorr, I made the change to set it to be True.
Also added a docstring for the download_file function.
@qhuang872
qhuang872 / reset.less
Created March 7, 2017 07:24 — forked from joshuapowell/reset.less
LESS: Global LESS CSS reset that gives elements default formatting.
/**
* Global Reset of all HTML Elements
*
* Resetting all of our HTML Elements ensures a smoother
* visual transition between browsers. If you don't believe me,
* try temporarily commenting out this block of code, then go
* and look at Mozilla versus Safari, both good browsers with
* a good implementation of CSS. The thing is, all browser CSS
* defaults are different and at the end of the day if visual
* consistency is what we're shooting for, then we need to