SOLVED: verify string is empty lazarus / 61-Beginners ---------------------------------------------------------------------- tudi_x · 2020-05-06 16:40 hi All, kindly please advise for verifying a string is empty what should be used? which of the below would be faster? A. string = "" B. length(string) = 0 thank you ---------------------------------------------------------------------- howardpc · 2020-05-06 16:43 I think they are pretty much identical in terms of code generated (though I have not checked). ---------------------------------------------------------------------- tudi_x · 2020-05-06 17:01 from a compiler perspective for B, does it need to convert it to string? or it just checks the length in memory? ---------------------------------------------------------------------- Remy Lebeau · 2020-05-06 17:15 Quote from: tudi_x on May 06, 2020, 06:40:52 pmkindly please advise for verifying a string is empty what should be used? which of the below would be faster? A. string = "" B. length(string) = 0 You could just look at the generated codegen for yourself, and/or profile the speed.  But in general, A should be faster, because it is just a simple comparison to see if the string's data pointer is nil (string = '' and string = nil do the same thing), whereas B requires checking if that pointer is nil and if not then dereferencing it to access the length stored in the string's metadata.  Unless the compiler is smart enough to recognize a check for Length=0 and optimizes it into a nil-pointer comparison. Quote from: tudi_x on May 06, 2020, 07:01:38 pmfrom a compiler perspective for B, does it need to convert it to string? or it just checks the length in memory? A String knows its own length, it is stored in the string's metadata in memory.  Length() simply returns that value as-is. ---------------------------------------------------------------------- tudi_x · 2020-05-06 17:18 understood. thank you very much! ---------------------------------------------------------------------- Remy Lebeau · 2020-05-06 17:23 Quote from: tudi_x on May 06, 2020, 07:18:33 pmso this would imply B. option to be faster? No, I would expect A to be faster. string = '' (or string = nil) is equivalent to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if (Pointer(string) = nil) then ... Length(string) = 0 is equivalent to (assuming no optimization): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if (Pointer(string) <> nil) then  len := PStrRec(Pointer(string))-1)^.lengthelse  len := 0;if len = 0 then ... ---------------------------------------------------------------------- PascalDragon · 2020-05-07 07:17 Remy Lebeau is right. Comparing against an empty string is faster. Though I wonder if I could get the compiler to handle Length(s) = 0 better as well... ---------------------------------------------------------------------- Remy Lebeau · 2020-05-07 17:28 Quote from: PascalDragon on May 07, 2020, 09:17:22 amThough I wonder if I could get the compiler to handle Length(s) = 0 better as well...  Just be careful, because some people like to use Length(s) > 0, and some people like to use Length(s) <> 0, when testing if a string is not empty. I suppose if the compiler can detect that Length(s) is being called (on the left or right side) for (Ansi|Unicode|Wide)String, and the result is being compared to 0 (on the other side), then generate code that performs a Pointer(s) = nil or Pointer(s) <> nil comparison instead, based on how Length(s) is being compared to 0, eg: Length(s) = 0 : Pointer(s) = nil Length(s) <> 0 : Pointer(s) <> nil Length(s) > 0 : Pointer(s) <> nil Length(s) < 0 : Length(s) < 0 or error? And maybe then do the same thing for dynamic arrays, too.