Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to improve the performance of Perl

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to improve the performance of Perl, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Variables and constants

1. Minimize the evaluation of mathematical expressions when improving Perl performance, such as:

$day=24*60*60;# is not good

$day=86400;#better

2. Use vec functions instead of variables to store very small numbers.

Print

1. If there are no variables in the output that need to be interpolated, use single quotation marks instead of double quotation marks because double quotation marks force Perl to check for information that may be inserted

2, use for multi-segment output instead of.. Because of the join operator. The strings are concatenated first and then printed as a parameter

Avoid unnecessary quotation marks

It is not absolutely necessary not to use quotation marks when improving Perl performance:

My$copy= "$large_string"

The above will make two copies of $large_string (one to $copy and the other to quotation marks), and vice versa

My$copy=$large_string

Make only one copy.

Array serialization

Also extracted from FAQ3.16

For large array serialization:

{local$,= "\ n"; print@big_array;} saves more memory than either of the following printjoin "\ n", @ big_array; # or {local$ "="\ n "; print" @ big_array ";}

Quote\

If you use large arrays or hash tables as arguments to functions when improving Perl performance, you should use a reference to them instead of using them directly. By using references, you can tell the function a pointer to the information. If you don't use references, you need to copy the entire array or hash table to the function's call stack, and then copy it again in the function. References can also save memory (which reduces footprint and administrative load) and simplify your programming.

Cycle

Place conditional statements in the loop as early as possible so that Perl does not execute useless statements. Such as

While () {

Chomp

Nextif/ ^ # /

Next can be placed on top of chomp.

Selective use of map and grep

Because map and grep use the LIST list parameter, do this

@ wanted=grep {/ pattern/}

Will read the entire file at once. For large files, it is better to use loops:

While () {

Push (@ wanted,$_) if/pattern/

}

Regular expression

Pack/unpack > regexp > substr

When deleting characters from a string, use tr///d instead of sqqqqqqqg

Use the "or" or "| |" action outside the regular expression.

$found=if/one/ | | / two/;#better

$found=if/one | two/;#useabovetoreplace

If the string is long and the regular expression is complex, you can use study to speed it up

Data structure

Tie::SubstrHash can be helpful for some types of data structures

= pod

If you use a large chunk of pod to describe your code when improving Perl performance, try not to put it in the top or middle of the file. Although the perl parser can skip pod quickly, it's not magic and it takes a while. It still needs to read it from disk, and the purpose of reading is simply to ignore it. Put all the pod after _ _ END__ so that the Perl compiler won't notice it.

But it might be a good habit to put pod with related code.

Warnings/strict

It is strongly recommended that you turn it on when programming and debugging, and remove them when the code is released.

Final

* it is strongly recommended that you read "Whenperlisnotquitefastenough" in the reference.

These are all the contents of the article "how to improve the performance of Perl". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report