瞎聊:Python2.x 和Lua中的字符串

zombie posted @ Fri, 09 Jul 2010 07:21:19 +0800 in Python with tags python Lua string , 3194 readers

Python2.x 中的String对象的实现让我觉得有点不可思议。Python的设计者为了提高内存利用率,使用了intern的机制来共享相同的字符串,也就是说如果你有两个字符串: s1='python' s2='python's1s2实际上是指向同一个内存块的,他们共享了同一个PyStringObjectPython内部维护了一个dict类型的变量interned来记录所有的共享字符串,这就引出了一个问题。由于共享字符串的维护直接依赖于Python中的dict,对interned查询需要的参数就应该是一个PyObect的‘派生’类型,不管interned中是不是已经存在了需要的字符串对象,Python总是会先建立一个零时的字符串对象,然后再以该零时对象为interned的一个key去查找是否存在一个已缓存的相同的字符串,如果存在的话就丢弃这个零时变量而是用interned中的缓存。这里最大的问题是在建立这个零时的字符串对象时有一个拷贝内存的过程: Py_MEMCPY(op->ob_sval, str, size+1);如果当前要创建的字符串还没有被缓存过,那么这里一切都很好;但是如果这个字符串已经缓存过了,这次所做的内存拷贝就是无用功,而且还是个耗费时间的无用功。如果字符串比较大,这里对效率的影响可想而知。

 

针对这个问题,最直接的想法是能不能不拷贝字符串而直接去根据参数中传进来的字符串去判断这个字符串有没有被缓存过了,这样做的话也就意味着不能再使用Pythondict来维护这些字符串缓存了,这可能就需要引入专门的代码来操作一个专用的数据结构了。相比较而言,Lua中使用一个stringtable来缓存所有字符串,创建一个新字符串的时候是先根据源字符串计算出一个hash值,再根据这个hash值去stringtable中查找,只有在这找不到时才会真正的创建一个通过拷贝创建一个新字符串。所以Lua中的字符串就没有这个问题。

 

Python的设计者肯定是意识到这个问题的,这应该也是他们权衡后的决策结果。这里纯粹就是一个概率游戏了,碰到某些极端应用时这个实现的效率可能会让人无法接受。这个问题也许只是Python广为诟病的速度问题的冰山一角。

 

 

doyle said:
Jul 09, 2010 05:40:06 PM

Python广为诟病的速度问题?

Rylan Landon said:
Dec 29, 2018 06:19:18 PM

Definitely As i browse the application last week though My partner and i various brain to sort it out and from now on Need be to enjoy a book the application repeatedly considering it is especially well crafted. Crawley Accountants

shell shockers said:
Feb 15, 2019 11:13:43 AM

Oh! This article has suggested to me many new ideas. I will embark on doing it. Hope you can continue to contribute your talents in this area. Thank you.

daftar judi bola onl said:
Apr 02, 2019 07:01:11 PM

Thanks for sharing this useful information here

daftar akun bola onl said:
Apr 06, 2019 08:26:55 PM

This is a very great post and the way you express your all post details that is too good

worldvaping said:
Dec 07, 2019 02:13:16 PM

Getting the most effective DNA boards is not a stroll in the park. However, Evolv has attempted to make the procedure simpler.

worldvaping said:
Dec 07, 2019 02:13:41 PM

Getting the most effective DNA boards is not a stroll in the park. However, Evolv has attempted to make the procedure simpler.

best economics tuiti said:
Dec 09, 2020 11:06:13 PM

Thank you for taking the time to publish this information very useful!

sk said:
Mar 19, 2021 09:55:45 PM

Nice to be visiting your blog again, it has been months for me. Well this article that i’ve been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share. Chemistry tuition

quaternian said:
Mar 22, 2021 11:57:36 PM

New web site is looking good. Thanks for the great effort.

Bridgewater associat said:
Mar 26, 2021 11:19:32 PM

An fascinating discussion is value comment. I think that it is best to write extra on this matter, it won’t be a taboo topic however generally people are not enough to talk on such topics. To the next. Cheers

บาคาร่า said:
Mar 30, 2021 09:06:32 PM

As casino games are turning to the online scene, you will really enjoy playing online baccarat from the comforts of your home. In fact, with readily online access by most people, you can play this classical game noted for originating in France. The convenience of baccarat online can really give you a taste of your favorite casino entertainment. Here are some advantages of playing baccarat online. Explore the different possibilities in order to benefit from baccarat being an enjoyable casino game in the comfort of your home.

ชุดสังฆทาน said:
Apr 07, 2021 11:27:14 PM

Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know.

sk said:
Apr 24, 2021 02:17:16 AM

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. สมัครเล่น จีคลับ 666

sk said:
Jun 11, 2021 11:04:27 PM

Thank you because you have been willing to share information with us. รีวิวเว็บพนัน UFA365

sk said:
Jun 14, 2021 12:59:29 AM

Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. Melhor IPTV

미프진 said:
Jun 17, 2021 03:24:33 AM

Your work is truly appreciated round the clock and the globe. It is incredibly a comprehensive and helpful blog.

poker88 said:
Jun 17, 2021 11:46:15 AM

Efficiently written information. It will be profitable to anybody who utilizes it, counting me. Keep up the good work. For certain I will review out more posts day in and day out.

sk said:
Jun 17, 2021 03:21:55 PM

i am always looking for some free stuffs over the internet. there are also some companies which gives free samples. หวยหุ้นไทย

kamagra oral jelly e said:
Jun 28, 2021 11:51:21 AM

Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks.

sk said:
Jul 02, 2021 01:21:01 PM

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. Donny65915

sk said:
Jul 19, 2021 11:22:21 AM

Nice knowledge gaining article. This post is really the best on this valuable topic. หวยหุ้น

หวยฮานอยพิเศษ said:
Jul 20, 2021 12:51:05 AM

Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though

Escorts in Delhi said:
Jul 23, 2021 08:24:08 PM

The Horny babes are here to give the best fun that the clients are looking to have.

mahnoor said:
Jul 29, 2021 07:08:41 PM This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.. Royal Online V2
marital counseling said:
Aug 17, 2021 02:44:50 AM

A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post.

 allergy clinic said:
Aug 21, 2021 01:16:55 AM

Great survey, I'm sure you're getting a great response.

sk said:
Sep 02, 2021 06:58:36 AM

I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. สมัคร bacc6666

dankwoods said:
Sep 09, 2021 10:40:32 PM

To begin,We created Dankwoods to quicken the process of smoking backwoods for even the most experienced roller. So throw your Swishers in the trash. this is made from an all natural Meduro backwood leaf wrapped around.

sk said:
Sep 10, 2021 06:38:00 PM

Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign. indian visa for british citizens

keta kuprė said:
Sep 13, 2021 11:41:29 PM

Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon!

Sputtering Targets said:
Sep 17, 2021 01:30:45 PM

Thanks for sharing this information. I really like your blog post very much. You have really shared a informative and interesting blog post with people..

sk said:
Sep 21, 2021 02:51:11 AM

Buy Dankwoods Online. Dank woods pre-rolls bring you the highest quality medical cannabis in a convenient, easy to smoke product. We take the best flowers sourced from local, trusted growers, and roll them into our all-natural papers. dankwoods

home cleaning edinbu said:
Oct 28, 2021 02:57:31 AM

Yeah bookmaking this wasn’t a bad decision great post! . home cleaning edinburgh

tecnológico said:
Oct 28, 2021 10:00:31 PM

Yeah bookmaking this wasn’t a bad decision great post! . tecnológico

seo said:
Nov 13, 2021 05:49:47 PM

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future... จีคลับ777

Home depot health ch said:
Nov 14, 2021 11:42:09 PM

Yeah bookmaking this wasn’t a bad decision great post! . Home depot health check

online casino real m said:
Nov 16, 2021 05:44:58 PM

Yeah bookmaking this wasn’t a bad decision great post! . online casino real money

online casino real m said:
Nov 16, 2021 09:34:38 PM

Largest lover messages were made to share it with your and gives honour of the bride and groom. Very sound systems facing unnecessary throngs of people should take into account each of our valuable concept of all presenting, which is one’s trailer. best man toasts online casino real money canada

maison au senegal said:
Dec 06, 2021 01:58:45 PM

Yeah bookmaking this wasn’t a bad decision great post! . maison au senegal

SEO said:
Jan 10, 2022 07:05:35 PM

This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful โรงงานผลิตครีม

baccarat said:
Jan 14, 2022 09:15:44 PM

Yeah bookmaking this wasn’t a bad decision great post! . baccarat

baccarat said:
Jan 16, 2022 03:07:14 AM

I am just commenting to let you know of the perfect experience my wife's princess encountered studying your web site. She picked up numerous details, most notably what it's like to have an ideal helping character to have many more very easily gain knowledge of selected advanced subject matter. You undoubtedly exceeded our own expectations. Thanks for offering such effective, healthy, explanatory and in addition fun thoughts on this topic to Gloria. baccarat

บาคาร่า said:
Jan 16, 2022 05:31:34 AM

Often the Are generally Weight reduction plan is unquestionably an low-priced and flexible weight-reduction plan product modeled on individuals seeking out shed some pounds combined with at some point maintain a far healthier your life. la weight loss บาคาร่า

SEO said:
Feb 27, 2022 06:25:00 AM

Admiring the time and effort you put into your blog and detailed information you offer!.. รับรูดบัตรเครดิตเป็นเงินสดออนไลน์

SEO said:
Mar 03, 2022 04:58:40 AM

I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. list of hospitals in japan

SEO said:
Mar 09, 2022 05:40:44 AM

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!THANKS!!!!!! 메이저놀이터

SEO said:
Mar 10, 2022 03:56:40 AM

Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon. 토토사이트

SEO said:
Mar 11, 2022 03:01:48 AM

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. 안전놀이터

SEO said:
Mar 13, 2022 05:08:43 AM

Please continue this great work and I look forward to more of your awesome blog posts. jili slot

SEO said:
Mar 17, 2022 01:26:26 PM

I know your expertise on this. I must say we should have an online discussion on this. Writing only comments will close the discussion straight away! And will restrict the benefits from this information. SOCIAL MEDIA MARKETING AGENCY MANCHESTER

seoo said:
Mar 19, 2022 11:53:30 PM

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. 토토사이트

SEO said:
Mar 28, 2022 01:48:54 AM

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. สูตร เสือมังกร fifa55

Hibbah said:
Mar 30, 2022 10:39:21 PM

When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style. rekrutacje it

seoo said:
Mar 31, 2022 03:11:58 AM

Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also voyance par telephone gratuite sans attente

SEO said:
Apr 02, 2022 03:02:02 AM

Wow i can say that this is another great article as expected of this blog.Bookmarked this site.. 의정부출장마사지

Hibbah said:
Apr 02, 2022 09:52:49 AM

i am always looking for some free stuffs over the internet. there are also some companies which gives free samples. FACEBOOK ADS AGENCY MANCHESTER

SEO said:
Apr 03, 2022 06:32:48 AM

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks الب ارسلان

ali said:
Apr 03, 2022 07:19:09 PM

I like review sites which grasp the cost of conveying the fantastic helpful asset for nothing out of pocket. I genuinely revered perusing your posting. Seriales Online

ali said:
Apr 04, 2022 06:46:04 PM

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. Espanol Doramas

ali said:
Apr 05, 2022 06:35:46 PM

It looks like the only spot that time steps on as it goes through, so it looks trodden and bleak. They look like ruthless places to me, and thus solitary. Turske Online

Hibbah said:
Apr 06, 2022 11:45:56 AM

online slots are prepared for all of us to open up a brand new enjoy in gambling slots XO on line, which has greater than a hundred video games to pick from and each recreation has a jackpot on the way to win, that's greater than 1 million baht. สมัคร slotxo

seoo said:
Apr 06, 2022 05:06:36 PM

I would like to say that this blog really convinced me to do it! Thanks, very good post. 메이저사이트

SEO said:
Apr 09, 2022 10:12:31 PM

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. 인천출장안마

seoo said:
Apr 10, 2022 06:39:37 PM

I am definitely enjoying your website. You definitely have some great insight and great stories. مسلسل جوقة عزيزة

Hibbah said:
Apr 15, 2022 12:23:09 AM

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!THANKS!!!!!! biuro nieruchomosci szczecin

SEO said:
Apr 16, 2022 04:39:09 PM

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. invention prototype companies

ali said:
Apr 19, 2022 12:59:46 AM

I know your aptitude on this. I should say we ought to have an online discourse on this. Composing just remarks will close the talk straight away! What's more, will confine the advantages from this data. Seriale Turcesti

Hibbah said:
Apr 20, 2022 12:06:58 AM

We are a 100% prison Thai on line income organization, 100% bt unfastened organization with international elegance excellent products. It is an modern product from Far Infrared Glutathione Complex Innovation that has obtained global awards. the icon

ali said:
May 14, 2022 06:32:49 PM

I woke up to be notified by this blog and I already knew my day started in right way! Your words of wisdom are overflowing and I truly appreciate it! Turske Online

Ali said:
May 17, 2022 05:49:17 PM Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have. Ver Anime Online
Ali said:
May 19, 2022 05:48:46 PM

It looks like the only spot that time steps on as it goes through, so it looks trodden and bleak. They look like ruthless places to me, and thus solitary. novelas online

Ali said:
Jun 12, 2022 03:55:23 PM

I found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information. KShows

ali said:
Jun 29, 2022 06:07:30 PM

I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. DoramasMp4

Ali said:
Jul 01, 2022 04:17:03 AM Einer der grössten Edelstein-Shops mit ständig mehreren 100.000 Edelsteine lagernd 45 Tage Rückgaberecht – Kauf auf Rechnung – alle Edelsteine mit Zertifikat eigenes gemmologisches Edelsteinlabor. Edelsteine
ali said:
Jul 02, 2022 04:59:15 PM

An interesting dialogue is price comment. I feel that it is best to write more on this matter, it may not be a taboo topic however usually individuals are not enough to talk on such topics. To the next. Cheers. GEM TV Serial

ali said:
Jul 23, 2022 03:19:45 PM

Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. Serialelatimp

ali said:
Jul 28, 2022 03:11:46 PM

I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more. koreanturk

ali said:
Jul 30, 2022 03:29:18 PM

It is perfect time to make some plans for the future and it is time to be happy. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it! lottery sambad

ali said:
Aug 12, 2022 05:28:39 PM

If more people that write articles involved themselves with writing great content like you, more readers would be interested in their writings. I have learned too many things from your article. Türkanime

ali said:
Aug 13, 2022 04:26:33 PM

Well this is such a shocking web journal you talk about her. I can without a ton of a stretch handle and the revelation of your blog is particularly solid. So a devotion of thankfulness is with the objective for offering to this incredible blog. dramacool

Junior Dakil Result said:
Sep 03, 2022 10:03:42 PM

Bangladesh Education Board DPE has conducted the class 8th grade of Junior School Certificate Exam and Junior Dakhil Certificate Exam on 1st to 15th November 2022 at all centers in division wise under Ministry of Primary and Mass Education (MOPME), and the class 8th grade terminal examination tests are successfully conducted for all eligible JSC/JDC students for the academic year of 2022. Junior Dakil Result jessore Board The Bangladesh government Minister of Secondary Education is going to announce the JSC Result 2022 in student wise for division students in education board wise, and the result documents will be submitted to the Prime Minister of the country after then the result with mark sheet will be announced to public to check the individual result.

rimaakter said:
Nov 13, 2023 01:09:41 PM

Kenhacai5 Cá Cược Thể Thao Trực Tuyến

 

Kèo Nhà Cái Ngon là một trang web cá cược uy tín và chất lượng giúp các bạn trải nghiệm những trận đấu và cơ hội cá cược không giới hạn Cá cược thể thao trực tuyến ngày càng trở nên phổ biến đối với những người đam mê thể thao cũng như những người đặt cược thông thường. Với sự tiện lợi của việc đặt cược thoải mái ngay tại nhà riêng, nhiều lựa chọn cá cược và cơ hội thắng lớn, không có gì ngạc nhiên khi ngày càng có nhiều người chuyển sang nền tảng trực tuyến cho nhu cầu cá cược thể thao của họ. Một trong những nền tảng đã được công nhận trong những năm gần đây là Keonacai5, một trang cá cược thể thao trực tuyến đáng tin cậy. vui lòng truy cập vào đây  kèo nhà cái 5 để biết thêm thông tin.

ali said:
Nov 21, 2023 04:52:31 AM

Welcome to delphox, Your awesome source for Informative Blog Content and Trending News. Delphox.co.uk is a multi-niche Blog and News website managed by our Community. delphox


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter