RightString code working for Unicode and ASCII?

Just starting out? Need help? Post your questions and find answers here.
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: RightString code working for Unicode and ASCII?

Post by skywalk »

Fred wrote::?:
I don't like Visual Designers, so I made a GUI creator from gadget settings / elements in multiple DataSections per window.
But, while writing that code, I found myself searching for the #PB_gadget _xxx constants and their values quite often.
For cases where the Constant I wanted didn't exist, I created one, but I didn't want that to conflict with existing ones.
So, why must my only way to browse through available constants be the dang'd Structure Viewer?(not well-named by the way)...
And if PB versions change the Constants values that I am referring to, then I will take care of that with a Tool checker.
Please?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: RightString code working for Unicode and ASCII?

Post by charvista »

Thank you Fred. This is highly logical. Even when #True becomes -50.87, it will still be the constant #True.
Those who are relying to the real value of a constant should use their own constants. A good idea is to use another language whenever possible, like #Vrai (1) and #Faux (0) in French, or #hai (1) and #iie (0) in Japanese :mrgreen:

Now back to this Unicode topic. HeXOR's solution works fine in Ascii and Unicode. A small question about the backslash in Unicode: it seems that the Japanese Windows system shows "\" as the Yen symbol. Any idea why? The backslash is Chr(92), but the Yen is Chr(65509)...

PS: Perhaps you know that the Japanese "hai" and "iie" are freely translated with "yes" and "no". But in fact, they mean "affirmative" and "negative", so you can think about "true" and "false". When a Japanese asks you a negative question, like "Have you not eaten yet?", and if you have not eaten yet, you should answer "no" in English, but in the Japanese language you should reply with "hai", because it is TRUE that you do NOT have eaten yet. Be careful if you are starving!!!! :wink:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: RightString code working for Unicode and ASCII?

Post by skywalk »

Trond wrote:SizeOf() is a compile-time function, it doesn't affect execution time.
Is this true for ArraySize() also?
I am always breaking out an integer variable to hold the ArraySize() or SizeOf() or whatever? :oops:
I'd love these nuggets of speedy wisdom all captured in the manual?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: RightString code working for Unicode and ASCII?

Post by Demivec »

skywalk wrote:
Trond wrote:SizeOf() is a compile-time function, it doesn't affect execution time.
Is this true for ArraySize() also?
I am always breaking out an integer variable to hold the ArraySize() or SizeOf() or whatever? :oops:
I'd love these nuggets of speedy wisdom all captured in the manual?
No, ArraySize() executes at run-time because arrays are dynamically sized. Structures and variables are not dynamically size, thus SizeOf() is done at compile-time.

ListSize() specifies that it uses a cached value and so it should be faster than ArraySize().

Here is the result of some simple speed tests I ran to determine if there is any noticeable difference.

Code: Select all

---------------------------
ListSize, ArraySize, SizeOf Vs Var
---------------------------
ListSize(lis())     672 ms
ArraySize(arr())    843 ms
SizeOf(Integer)     328 ms
Var                 344 ms
The tests consisted of separate loops where an integer variable was set equal to one of the above values. The loops had 100000000 repetitions.

Based on the test results it seems like a good idea to store the result of ListSize() and ArraySize() in a variable to increase speed for frequent accesses or time critical code.
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: RightString code working for Unicode and ASCII?

Post by skywalk »

Thanks Demivec!
I should have made the "dynamic" connection with ArraySize(). :oops:
But, it is interesting to see the relatively small hit for setting SizeOf() to a var.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: RightString code working for Unicode and ASCII?

Post by Trond »

skywalk wrote:Thanks Demivec!
I should have made the "dynamic" connection with ArraySize(). :oops:
But, it is interesting to see the relatively small hit for setting SizeOf() to a var.
Using SizeOf() actually takes the same time as using a number (a = 2). And remember that these times includes the time it takes to update and check the loop counter. In fact, using an empty loop takes about the same time as setting the variable in it:

Code: Select all

CompilerIf #PB_Compiler_Debugger
  CompilerError ; Turn off the debugger
CompilerEndIf

#Tries = 1000000000

time = ElapsedMilliseconds()
For U = 0 To #Tries
  var = SizeOf(Character)
Next
MessageRequester("SizeOf()", Str(ElapsedMilliseconds()-time))

time = ElapsedMilliseconds()
For U = 0 To #Tries
  var = 2
Next
MessageRequester("2", Str(ElapsedMilliseconds()-time))

time = ElapsedMilliseconds()
For U = 0 To #Tries
  ; do nothing
Next
MessageRequester("Do nothing", Str(ElapsedMilliseconds()-time))
Probably due to code alignment coincidences, the empty loop actually takes a longer time on my computer than the others.
Post Reply