Log writing function and continuous optimization, log function

Source: Internet
Author: User

Log writing function and continuous optimization, log function

First, let's look at the following functions:

<Span style = "font-size: 18px;"> void writelog (char * file, char * msg) {FILE * fp = NULL; int nDataLen = strlen (msg ); fp = fopen (file, "AB +"); // read only if (fp! = NULL) {fwrite (msg, nDataLen, 1, fp) ;}fclose (fp); // close the file} </span>

What's the problem? If fp is empty, Will fclose (fp) be executed )?

Modify as follows:

<Span style = "font-size: 18px;"> void writelog (char * file, char * msg) {FILE * fp = NULL; int nDataLen = strlen (msg ); fp = fopen (file, "AB +"); // read only if (fp! = NULL) {fwrite (msg, nDataLen, 1, fp); fclose (fp); // close the file }}</span>

Is that okay?

Is there a problem when calling this log function in multiple threads?

So one method is to lock, but the efficiency may be lower.

Another method is to use a thread to receive logs written by other threads, and a thread to write logs.

In this way, the working thread will not be blocked, which can improve the concurrency efficiency.

 

The process of programming and design is a step-by-step process. No one will think about it very comprehensively at first, and there is no absolutely wrong code or design. If there are obvious functional errors, they will be corrected during the design process. For example, what is the probability of fp being null in the first function? Therefore, no error occurs. When the log function is only used in a single thread, it is unnecessary to consider locking and multithreading messages. Therefore, it depends on the application scenario.

Programmers are disgusted with the changes in demand. The requirement is the application scenario. Only when the requirements are confirmed can we design a relatively matched scheme, which is not necessarily perfect, but it must be optimal when various factors are integrated. The cycle should be considered, and the cost should be considered.

 

Reprinted please indicate original link: http://blog.csdn.net/wujunokay/article/details/38310669

 



 

 


Optimization for compiling Matlab Functions

The key is to see what function f is.

======================================

M = magic (5 );
X = M (2: end ,:);
Y = M (1: end-1 ,:);
Ii = 1: size (X, 2 );
Jj = 1: size (X, 1 );
[II, JJ] = meshgrid (ii, jj );
F = zeros (size (X ));
I = find (II + 2 * JJ> 0 );
F (I) = 2 * X (I) + Y (I );
I = find (II-JJ> 0 );
F (I) = X (I );

Here there are two problems, one is that your loop is I = 1: n, and the loop references M (I-1, j), will cause the array to cross-border; the other is that in the expression f, I + 2 * j> 0 is always set, so that the next two are equal to the white write.

What is function optimization?

Function optimization refers to the optimization of operation efficiency. The optimized function execution speed should be higher than the optimization speed.

For example, this function runs the calculation result from the specified number v to the specified max.
However, the writing efficiency is not high, because it is gradually accumulated using the for loop. For
For those who are better at math, only one line of code is a formula.
Function test (v: Int, max: Int ){
For (I = 0; I <max; I ++ ){
V = v + I;
}
Return v;
}

For such functions, if the max value is not very large or the call frequency is very high, it will not affect the speed of the main program. However, if the max value is large and the function needs to be called multiple times per second, the execution efficiency difference between optimization and non-optimization is exponentially different.

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.