45,000 times slower!
To compare the computation performance between Python and C languages, let’s do a loop for sum in one second. The code itself is pretty much self-explanatory.
Run:
time python python_loop.py 10000000
real 0m1.044s
user 0m1.004s
sys 0m0.004s
It is 10 millions loops in a second for Python. It sounds not bad.
Compile in normal mode:
gcc c_loop.c -o c_loop
Run:
time ./c_loop 450000000
real 0m1.032s
user 0m1.008s
sys 0m0.008s
It is 450 millions loops in a second, which is 45 times faster than Python.
Furthermore, C can be compiled in optimized mode for a better performance. …
A collection of my favorite Python debugging options.
import time
print(time.asctime(),'debugging ...')
Output:
Sat Jul 4 13:45:52 2020 debugging ...
import logging
logging.basicConfig(filename = 'a.log')
log = logging.getLogger()
log.error('Some error')
cat a.log:
ERROR:root:Some error
import logging# DEBUG, INFO, WARNING, ERROR, CRITICAL
log_level = logging.INFO
logging.basicConfig(filename = 'b.log',
level=log_level,
filemode='w', # or 'a'
format='%(asctime)s %(levelname)s: %(message)s',
)log = logging.getLogger()
log.info('Some info log')
log.debug("Won't print at INFO level")
cat b.log:
2020-07-04 17:50:42,953 INFO: Some info log
Use different logger names (logname) for different log files.
import logginglog_level = logging.INFO
def create_logger(filename, logname=''):
handler = logging.FileHandler(filename)
formatter = logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s'
)
handler.setFormatter(formatter)
log = logging.getLogger(logname)
log.setLevel(log_level)
log.addHandler(handler) …
Let’s start with a perfect concurrent example #1.
Async function say_after is an example from Python official documentation. It prints something ‘what’ after sleeping ‘delay’ seconds.
In the main function, we create two tasks of say_after, one says ‘hello’ after 1 second and the other say ‘world’ after 2 seconds. Run it and we see it takes 2 seconds in total because both tasks run concurrently. Perfect!
This post will explain how to set up an app password in Google account and use Python to send emails in a few lines of code for automatic reporting, test automation or CI/CD failure notification etc.
To use a Gmail account to send emails with a third party app, e.g. Python script, in this case, we need to set up an app password. For security reasons, the normal Gmail password is restricted to web login only. …
There are many open-source face recognition packages like face_recognition which you can easily install on Linux servers. But it is very difficult or impossible to deploy them on mobile and IoT devices. One option is to use machine learning mobile frameworks such as TensorFlow Lite to call pre-trained models.
But are there easier options? Yes! With 5G coming, it will take only 0.01 second to upload a 100KB image at a speed of about 100Mbps, so we can deploy almost everything including face recognition as a service on server-side and a light app on client-side. …
Have you ever used bank/payment app face login and been asked to open mouth, nod or turn head? Such methods have been very popular, especially in China, to prevent deceiving/hacking using static face images or 3D prints. I spent about two days and finally figured out a quite simple way to detect mouth open utilizing the feature outputs from the face_recognition project.
Here is a quick look at the effect when applying the algorithm to real-time webcam video.
face_recognition is an awesome open source project for face recognition based on dlib, just as described by itself:
The world’s simplest facial recognition api for Python and the command…
Glances + Influxdb + Grafana are a good combination to monitor Linux server performance stats. I will show how to set it up on Ubuntu as an example.
These three components work together as below.
I was reading a machine learning book and learned that edges are important feature inputs for machines to learn if there is an object in the picture, a face in this case. Look at the figure with only edges on the left, you can easily tell it is a face by human eyes, isn’t it? That helps machines the same way.
Originally I thought finding edges itself requires some ‘Artificial Intelligence’. But I remember Python PIL library has find_edges filter, which definitely is not a machine learning function. …
There are three methods to format a string in Python:
Note: From version 3.1, the positional argument specifiers can be omitted for str.format(), so '{} {}'.format(a, b)
is equivalent to '{0} {1}'.format(a, b),
though the method was firstly introduced in version 2.6.
I was confused when I started learning Python. If you have the same feeling, I recommend you have a read of Roberto Preste’s post first. He explains quite well what they are. Basically, they are equally effective and efficient. You can just pick one you like. …
Imagine you have written REST API function tests in Python, how to scale them up to do performance tests?
I have previously written an article about how to create REST API function tests using Python. In this article, I will continue to explain how you can use existing function tests and scale them up to do performance tests, using Python modules requests, threading, and queue.
Let’s use the same flask mock service endpoint I used for the functional tests, but just add a time.sleep(0.2)
to simulate network delay 0.2 seconds.
Save the code as a file, e.g. flask_mock_simple_service.py, …