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 realize string-to-array by perl

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge about how to achieve string conversion array in perl. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Question: if there is a string of numbers of type str, how to quickly convert it to an array?

I give four methods here and give a simple performance comparison.

Method 1:

% timeitl = [] for i in '123456789123456789123456789: l.append (int (I)) np.array (l)

Results: 11.2 μ s ±114 ns per loop (mean ±std. Dev. Of 7 runs, 100000 loops each)

Method 2:

% timeitnp.array ([i for i in '123456789123456789123456789'], dtype='int8')

Results: 7.12 μ s ±50.4 ns per loop (mean ±std. Dev. Of 7 runs, 100000 loops each)

Method 3:

% timeitnp.array (list ('123456789123456789123456789'), dtype='int8')

Results: 6.52 μ s ±52 ns per loop (mean ±std. Dev. Of 7 runs, 100000 loops each)

Method 4:

% timeitnp.fromiter ('123456789123456789123456789,' int8')

Results: 4.07 μ s ±33.2ns per loop (mean ±std. Dev. Of 7 runs, 100000 loops each)

The performance of method 1 has been improved from method 1 to method 4, and the performance improvement of method 4 is still very significant. The result of a little performance improvement in front of a large number of calculations may be the difference between a few minutes and hours or even days.

These are all the contents of the article "how to realize string-to-array in perl". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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

Internet Technology

Wechat

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

12
Report