Why is my Python so slow?

Updated on technology 2024-02-25
4 answers
  1. Anonymous users2024-02-06

    That's pretty much it. Multithreading is currently only used for network multithreading acquisition and performance testing.

    This is also the case in other languages, where the nature of threads limits the scope of application. Only when there is a surplus of CPUs and other tasks are slow, it is beneficial to use threads to balance the wait time and improve concurrency performance.

    The problem with threads is mainly the security and stability of threads. Threads cannot be forcibly aborted, and threads share memory with the main process, which may affect the memory management of the main process.

    In Python, a thread problem can cause the main process to crash. Although the threads in Python are the real threads of the operating system.

    So how to solve it? Through our use of the process way. When a child process crashes, all memory and error states are completely freed. So the process is safer. In addition, through the process, Python can bypass the global lock problem of GIL.

    But the process is also limited. Do not create processes that exceed the total number of CPU cores, otherwise it will not be efficient.

    To sum it up briefly.

    When we want to multitask, we should first think of using multiprocessing, but if we feel that the process is too cumbersome, then we should consider using threads. If you have too much to do in multitasking, consider multi-process, with each process multi-threaded. If you still don't want to deal with it, you need to use the polling mode, such as using poll event, twisted, etc.

    If it is a GUI mode, it needs to be handled through the event mechanism or the message mechanism, and the GUI uses a single thread.

    So don't use threads blindly in python, and don't abuse them. But it's a fact that threads are unsafe. If you're just doing a few background tasks, you might want to consider using daemon threads.

    If you need to do something dangerous and it might crash, use a subprocess to do it. If you need a service with high stability and a low number of concurrent requests. It is highly recommended to use the multiprocessing module for multiple processes.

    In Linux or UNIX, processes are not as expensive to use as Windows. It's still acceptable.

  2. Anonymous users2024-02-05

    It's a matter of your operation, find a place to study hard, and practice more, and it will be faster.

  3. Anonymous users2024-02-04

    This brings us to the main difference between Python and C languages, Python is an interpreted language, in layman's terms, you can type sentence by sentence, while Python interpreter can execute sentence by sentence, and C The language belongs to the compiled language, can not do this, can only be completed by one-time input, compiled into a complete program and then executed, and this compilation process due to the modern compiler has done a lot of optimization, and your program has no input only output, every run out of a fixed result, so it is very likely to be optimized by the compiler to only one output statement (the actual situation may be more complicated), in general, it is due to the difference in principle between the two that leads to the difference in performance, you can search for relevant information, turn off C compile-time optimizations, take a look at performance, or change fixed values to runtime inputs and see the effect.

    There are many advantages of Python over C, and you don't need to worry about performance, making a program complex enough that they are not much more efficient than each other.

  4. Anonymous users2024-02-03

    It's true that Python is a bit slow, but it's easy to use and easy to understand. The main thing the landlord said is that this method is originally an encapsulated function.,It takes time to call.,It's not as good as a direct three-loop loop.,If it's just to highlight the characteristics of python.,You can try this:

    list = [[x, y, z] for x in range(-100, 100) for y in range(-100, 100) for z in range(-100, 100) if(x != 0 and y != 0 and z !

    0 and 5 * x * y * z == 6 * x * y + y * z + z * x))]

    One line of ** is sufficient, and the efficiency is slightly improved, but it is still not comparable to assembly language c c++.

Related questions
11 answers2024-02-25

Turn on to enter safe mode with networking, open your QQ and space. Check the CPU usage. If it's different from you, it's poisonous. >>>More

13 answers2024-02-25

I'm using Battlefield 3 as a benchmark and your PC is pretty good at playing, so I think it's good. >>>More

13 answers2024-02-25

Fast. Start adding the Beastmaster, and add aiming after the coercion. Soon. No breaks.

18 answers2024-02-25

Generally speaking, the timeliness of YTO is basically controlled within 48 hours, sometimes because of the large number of air parts in other provinces, there will be flight delays or shuttle delays due to the weather, like now close to the end of the year is the peak period of express delivery, the company will have a burst situation, so the timeliness will be delayed. >>>More

9 answers2024-02-25

There's Sasuke! But just Naruto and Payne have been fighting for half a year, so it seems that Sasuke hasn't been there for a long time, and now that Sasuke is going to the Five Shadows Conference, it's going to be there soon, and it's estimated that I'll have to play for half a year, so let's watch it slowly when the time comes.