Menu
Learn
Documentation
Language Reference
Library Reference
Command-line Reference
Feature Overview
Articles
Downloads
Packages
Community
Blog
Orgs using D
Twitter
Calendar
Forums
IRC
Wiki
GitHub
Issues
Get involved
Contributors
Foundation
Donate
Sponsors
Resources
Tour
Books
Tutorials
Tools
Editors
IDEs
Visual D
Acknowledgments
D Style
Glossary
Sitemap
Search
Entire Site
Language
Library
Forums
Learn group
This thread
go
Forums
Forums
Forum Index
New users
Learn
Community
General
Announce
Improvements
DIP Ideas
DIP Devel.
Ecosystem
GDC
LDC
Debuggers
IDEs
DWT
Development
Internals
Issues
Beta
DMD
Phobos
Druntime
Study
Turkish
Genel
Duyuru
Log in
Settings
Help
Index
»
Learn
»
Utf8 to Utf32 cast cost
»
Post reply
Warning: the post you are replying to is from 10 years ago (June 10, 2015).
Posting to
Learn
in reply to
Marco Leise
Your name:
Your email address (
?
):
Subject:
Message:
On Wednesday, 10 June 2015 at 16:02:33 UTC, Marco Leise wrote: > Am Mon, 8 Jun 2015 12:59:31 +0200 > schrieb Daniel Kozák via Digitalmars-d-learn > <digitalmars-d-learn@puremagic.com>: > >> >> On Mon, 08 Jun 2015 10:41:59 +0000 >> Kadir Erdem Demir via Digitalmars-d-learn >> <digitalmars-d-learn@puremagic.com> wrote: >> >> > I want to use my char array with awesome, cool std.algorithm >> > functions. Since many of this algorithms requires like >> > slicing etc.. I prefer to create my string with Utf32 chars. >> > But by default all strings literals are Utf8 for performance. >> > >> > With my current knowledge I use to!dhar to convert Utf8[](or >> > char[]) to Utf32[](or dchar[]) >> > >> > dchar[] range = to!dchar("erdem".dup) >> > >> > How costly is this? >> >> import std.conv; >> import std.utf; >> import std.datetime; >> import std.stdio; >> >> void f0() { >> string somestr = "some not so long utf8 string >> forbenchmarking"; >> dstring str = to!dstring(somestr); >> } >> >> >> void f1() { >> string somestr = "some not so long utf8 string >> forbenchmarking"; >> dstring str = toUTF32(somestr); >> } >> >> void main() { >> auto r = benchmark!(f0,f1)(1_000_000); >> auto f0Result = to!Duration(r[0]); >> auto f1Result = to!Duration(r[1]); >> writeln("f0 time: ",f0Result); >> writeln("f1 time: ",f1Result); >> } >> >> >> /// output /// >> f0 time: 2 secs, 281 ms, 933 μs, and 8 hnsecs >> f1 time: 600 ms, 979 μs, and 8 hnsecs >> > > Please have the result of the transcode influence the program > output. E.g. Add the first character of the UTF32 string to > some global variable and print it out. At the moment - at > least in theory - you allow the compiler to deduce f0/f1 as > pure, return-nothing functions and you will benchmark anything > from your written code to an empty loop. I'm talking out of > experience here: > https://github.com/mleise/fast/blob/master/source/fast/internal.d#L99
Enable
Markdown
Copyright © 1999-2021 by the
D Language Foundation