Python uses ctypes to increase execution speed. the ctypes Library allows developers to develop with C language. This introduction of the C language interface can help us do a lot of things, such as some small issues that require calling C code to improve performance. You can access the kernel32.dll and msvcrt. dll dynamic link libraries on Windows and the libc. so.6 libraries on Linux. Of course, you can also use your own compiled shared library.
Let's look at a simple example. We use Python to calculate the prime number within 1000000, repeat this process 10 times, and calculate the running time.
Supplier and Memoize
SQLite is a common data storage method in Android. you must use SQLiteOpenHelper to access database data.
A good database connection code should be able to solve the following problems: a) building an instance is more expensive resources B) database connection is best to reuse c) methods such as onUpdate cannot conflict with other instances during execution.
This can be very simple.
Suppliers.memoize(new Supplier() { @Override public SQLiteOpenHelper get() { return new ...; }})
This code uses some auxiliary methods provided by Guava to implement Supplier and Memoize and logic. Supplier is generally used as factory, generator, builder, and closure. Memoize is similar to the concept of cache. once an instance is generated, the same instance will be returned in future calls, and the thread is secure.
There are several advantages to this writing. one is to build an instance only when necessary, and it does not block program execution at the beginning; the other is that it uses memoize to implement caching easily, ensure that only one instance is generated.
Code injection
Glow is a heavy user of code injection. it makes our code more structured, clear, simple, and saves a lot of development time.
Dagger 2 is our tool for implementing injection. if you are interested, you should go to the website to learn more about the related content. In addition to injection, it also has some additional features, which happen to be used for caching, and it is quite simple. we only need to use several additional annotation or interfaces.
@ Singleton
I believe that everyone should be familiar with this, which is a frequently asked question during the interview. Simply put, it is a singleton. For this reason, you don't have to worry about how to implement cache for these instances.
@Singleton public class SingletonClass { }
@ Reusable
This is a new and cool feature. Although the singleton is good, sometimes the instance may be too large to be stored in the memory and cannot be recycled. At present, the program may not be used, and it may be a waste of resources. In many cases, we do not have such strict requirements that we need a unique instance. we can reuse it if it can be reused. [email protected] scene. if there is already a generated instance, you just need to reuse it. it cannot be re-instantiated and does not need to be guaranteed.
@Reusable public class ReusableClass { }
Lazy
Lazy uses something different from the previous two. @ Singleton and @ Reusable are generally used in provides or type definitions, but Lazy is used for usage. the effect of Lazy is similar to that of Supplier and Memoize.
@Inject Lazy lazySQLiteOpenHelper;
This won't be a SQLiteOpenHelper instance until you start to call lazySQLiteOpenHelper. get (). Once the first instantiation ends, the first result will be returned for future calls.
Observable
In the process of using an app, a lot of data needs to be obtained from the server. In our app, we provide customized content for users every day, which will not change in the short term, and it takes too much time to retrieve the content from the server each time, however, it does not seem necessary to store data in persistent storage such as databases or files. After comprehensive consideration, it seems that the memory cache is a good choice.
Therefore, the cache needs to provide the following functions: first, it is a cache, and second, its structure needs to be very simple, because it needs to be used in many places. Again, it requires thread security.
Later, our implementation scheme was very simple, using some methods provided by Retrofit and Observable.
private static final long EXPIRE_MS = 5 * 60 * 1000; private Pair
cache; public synchronized Observable getDailyContent() { if (cache == null || cache.first + EXPIRE_MS < System.currentTimeMillis()) { cache = Pair.create(System.currentTimeMillis(), serverApi.getContent()); } return cache.second; }
The essence of this method is to use the Observable object returned by fit, and then the Observable will provide a cache-like method, so that the network request will not be sent before subscribe, but once the result is returned, later callers will get the same result.
Note
Although the cache is good, it is quick and convenient to use, you must pay attention to data updates and thread security during use and avoid dirty data.
From: http://www.jointforce.com/jfperiodical/article/3516