A small test of the object-C Programming tips-global_queue

Source: Internet
Author: User

During the previous construction of the basic network library, the encryption group package sent by the network and the callback unpacket returned by the network are processed in a separate main thread. The time consumed by the tracking program is quite large, so it is decided to use multi-threaded processing. At least the main thread should not be used.

I have been emphasizing the wonderful use of multi-thread dispatch on the Internet for a long time. I just want to use it without actually testing its performance. The results of the last test are sent out today. To be honest, the multi-thread efficiency of dispatch is indeed very high, which has a lot to do with the number of CPU cores.

Test ideas:

Use a self-written time-consuming function to record the time required for execution. Then, use dispatch_global_queue to record the time consumed for execution of 4, 6, 8, and 10 times to get the time spent. The basic code is very simple, as follows:

//// Main. M // threadtest // created by Lipeng on 14-7-23. // copyright (c) 2014 COM. hollance. all rights reserved. // # import <Foundation/Foundation. h> # include <sys/time. h> # define repeat_times 6/* shows how many cores your computer has. It is reasonable to say that repeat four times should be four times the time, but because it is a dual-core CPU, it only takes two times. */Typedef void * (* workfunc) (void); # define setfunc (func) # func, (workfunc) funcint gettime (char * Identify, workfunc func) {struct timeval TV _begin, TV _end, TV _diff; gettimeofday (& TV _begin, null); func (); gettimeofday (& TV _end, null); timersub (& TV _end, & TV _begin, & TV _diff ); long timecost = TV _diff. TV _sec * 1000 + TV _diff. TV _usec/1000; printf ("% 20 s cost % 10ld millseconds \ n", identify, timecost); Return 0;} int costtimefunc () {double M = 1.2, j = 3.4, K = 5.87; For (INT I = 0; I <3*3000000; ++ I) {J = m * K * m; k = m * j * k; M = K * j * k; k = I * j * m;} return 0;} void testonethread () {costtimefunc ();} void testtwothread () {dispatch_queue_t globalqueue = Queue (queue, 0); dispatch_sync (globalqueue, ^ {dispatch_apply (repeat_times, globalqueue, ^ (size_t index) {costtimefunc () ;}) ;}int main (INT argc, const char * argv []) {@ autoreleasepool {for (INT I = 0; I <10; ++ I) {gettime (setfunc (testonethread); gettime (setfunc (testtwothread) ;}} return 0 ;}

Modify # define's repeat_times to test the time taken to repeat different times. Test results show that when the CPU is dual-core, the time will be halved. When the CPU is quad-core, the time is about 1/4. However, the time on the I5 dual-core is also 1/4, because the I5 is a dual-core four-thread, and each core can run two threads at the same time. This indirectly proves the high efficiency of dispatch_global_queue.


Therefore, it is better to use dispatch_global_queue to process network requests. This not only avoids the blocking of the main thread, but also improves the processing efficiency of returning multiple simultaneous requests. In the response callback of the request, global_queue is first used to pre-process the result, unpack, decrypt, parse it into a data model, and then dispatch the last resolved content to the main thread. In this way, the main thread only cares about its interface logic processing, freeing it from the heavy data parsing work, and improving the efficiency.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.