Home > Backend Development > Python Tutorial > Python uses ctypes to improve execution speed

Python uses ctypes to improve execution speed

大家讲道理
Release: 2016-11-10 15:06:14
Original
1616 people have browsed it

ctypes library allows developers to develop with the help of C language. This interface that introduces C language can help us do many things, such as some small problems that require calling C code to improve performance. Through it, you can access the kernel32.dll and msvcrt.dll dynamic link libraries on Windows systems, and the libc.so.6 library on Linux systems. Of course, you can also use your own compiled shared library

Let’s look at a simple example first. We use Python to find prime numbers within 1,000,000, repeat this process 10 times, and calculate the running time.

Supplier and Memoize

SQLite is a commonly used data storage method in Android. When accessing database data, you need to use SQLiteOpenHelper.

A good database connection code should be able to solve the following problems: a) It is resource-intensive to build an instance b) It is best to reuse database connections c) OnUpdate and other methods cannot conflict with other instances during execution.

It can be easily written like this

Suppliers.memoize(new Supplier() { @Override public SQLiteOpenHelper get() { return new ...;
  }
})
Copy after login

This code uses some auxiliary methods provided by Guava to implement Supplier, Memoize and logic. As the name suggests, Supplier is generally used as factory, generator, builder, and closure. Memoize is similar to the concept of cache. Once it generates an instance, it will return the same instance in subsequent calls, and it is thread-safe.

Writing like this has several advantages. First, it only builds the instance when needed and does not block the execution of the program at the beginning. Second, it simply implements caching using memoize to ensure that only one instance is generated.

Code Injection

Glow is a heavy user of code injection. It makes our code more structured, clear and simple, while also saving a lot of development time.

Dagger 2 is our tool for injection. Interested students should go to the website to learn more about the relevant content. In addition to injection, it also has some bonus functions, which can be used by us to implement caching, and it is very simple. We only need to use a few additional annotations or interfaces.

@Singleton

I believe everyone should be familiar with this. This is a frequently asked question during interviews. Simply put, it is a singleton. Because, with it, you no longer have to worry about how to cache these instances.

@Singleton public class SingletonClass {  
}
Copy after login

@Reusable

This is a new cool feature. Although singletons are good, sometimes the instances may be too large, always stored in memory, and cannot be recycled. They may not be used by the program for the time being, so it feels a bit wasteful no matter what. In many cases, we do not have such strict requirements that we need only one instance. If we can reuse it, reuse it. In [email protected] scenario, if there is already a generated instance, just reuse it. Reinstantiation is not possible, and no guarantee is required.

@Reusable public class ReusableClass {  
}
Copy after login

Lazy

The place where Lazy is used is slightly different from the previous two. @Singleton and @Reusable are generally used in provides or type definitions, but Lazy is used when using it. Its usage effect is similar to the Supplier and Memoize mentioned at the beginning.

@Inject Lazy lazySQLiteOpenHelper;
Copy after login

The SQLiteOpenHelper instance will not be generated until you start calling lazySQLiteOpenHelper.get(). Once the first instantiation is completed, subsequent calls will return the first result.

Observable

In the process of using the app, a lot of data needs to be obtained from the server side. In our app, we provide users with some customized content every day. This content will not change in the short term. It is too time-consuming to retrieve it from the server every time, but it does not seem to be easy to store it in persistent storage such as databases or files. necessary. After all things considered, it seems that memory caching is a good choice.

So this cache needs to provide the following functions. First of all, it is a cache. Secondly, its structure needs to be very simple because it needs to be used in many places. Thirdly, it must be thread-safe.

Later, our implementation plan was very simple, using some methods provided by Retrofit and Observable.

private static final long EXPIRE_MS = 5 * 60 * 1000; private Pair<Long, Observable>
Copy after login
cache; public synchronized Observable getDailyContent() { if (cache == null || cache.first + EXPIRE_MS < System.currentTimeMillis()) {
      cache = Pair.create(System.currentTimeMillis(), serverApi.getContent());
    } return cache.second;
  }
Copy after login

The essence of this method is to use the Observable object returned by Retrofit, and then Observable will provide a cache-like method, so that the network request will not be issued before subscribe, but once the result is available, subsequent callers will Got the same result.

Attention

Although caching is good and fast and convenient to use, during use, everyone must pay attention to data updates and thread safety to avoid dirty data.

From: http://www.jointforce.com/jfperiodical/article/3516

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template