Skip to content

Comment on Never create Ruby strings longer than 23 characters

Comments

Unicode of course takes up more space and fills up your buffer sooner. Looks like the jump happens after 8 chars.

  Benchmark.bm do |bench|
    run("と", bench)
    run("がと", bench)
    run("りがと", bench)
    run("ありがと", bench)
    run("ありがとあ", bench)
    run("ありがとあり", bench)
    run("ありがとありが", bench)
    run("ありがとありがと", bench)
    run("ありがとありがとあ", bench)
    run("ありがとありがとあり", bench)
    run("ありがとありがとありと", bench)
    run("ありがとありがとありがと", bench)
  end

               user     system      total        real
  2  chars  0.210000   0.000000   0.210000 (  0.212420)
  3  chars  0.200000   0.000000   0.200000 (  0.199957)
  4  chars  0.200000   0.000000   0.200000 (  0.199356)
  5  chars  0.200000   0.000000   0.200000 (  0.199142)
  6  chars  0.200000   0.000000   0.200000 (  0.198047)
  7  chars  0.190000   0.000000   0.190000 (  0.198984)
  8  chars  0.190000   0.000000   0.190000 (  0.196917)
  9  chars  0.250000   0.000000   0.250000 (  0.245808)
  10 chars  0.240000   0.000000   0.240000 (  0.247153)
  11 chars  0.250000   0.000000   0.250000 (  0.248083)
  12 chars  0.250000   0.000000   0.250000 (  0.247753)
  13 chars  0.240000   0.000000   0.240000 (  0.250674)
Grabbed those unicode chars from http://blog.trydionel.com/2010/03/23/some-unicode-tips-for-r... no clue what that says.

I'm going to split some hairs, because it matters for the topic at hand.

>Unicode of course takes up more space and fills up your buffer sooner. Looks like the jump happens after 8 chars.

It sounds like you are conflating unicode with UTF-8. There is more than one way to represent the unicode code points, and UTF-8 is one of them. Further, it seems like you assume that "unicode characters" have a constant size. This is a potentially dangerous misunderstanding of how UTF-8 works. UTF-8 code points have a variable number of bytes (from one to four bytes, IIRC.) You happen to have copied some code points that take 3 bytes each.

The UTF-8 encoding scheme is a great compromise, and the wikipedia article is easy to follow: http://en.wikipedia.org/wiki/UTF-8

I also used to believe Unicode and UTF-8 were different types of encoding until someone corrected me. I just remembered why I had thought such a thing in the first place:

http://msdn.microsoft.com/en-us/library/system.text.encoding...

You and probably everyone else the first time they encounter unicode / UTF-8. I wonder if it's because both terms start with 'U'.

Thanks for correcting my errors.

> no clue what that says.

I think it's supposed to be ありがとう ("arigatou"), which is is Japanese for "thank you" (the informal version). The characters are hiragana.

I have no idea why they left off the う at the end, though, except that it just turns the last o into a long o and many foreign speakers are insensitive to the difference between the two because they sound almost the same.

It's pretty common to leave out the う in informal speech.

I don't use it. For Japanese, try Rikaichan (a Firefox translation plugin) which lets you look up unfamiliar kanji without leaving the website. You can also look words up on http://www.alc.co.jp/ but it might be hard to use unless you can read at least basic Japanese.

Yes, hiragana are from an unicode range that encodes to 3 UTF-8 characters each, so ruby appears to use UTF-8. ありがと is "arigato".

You probably mean "three bytes in UTF-8", talking about "UTF-8 characters" is really confusing.

You're absolutely right, I should have said "three bytes" or "three octets".

ありがとう -> thanks, note you are missing the う on the end.

You're welcome.

That's what I get for checking up on my limited unicode knowledge before posting, and then just C&Ping.

Thanks.

Cool! Thanks for taking the time to test this - while the idea of testing with unicode chars occurred to me while writing the post, I didn't have time to get to it...

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.