I write code in Java, where I disconnect based on whether it starts stringwith certain characters when the loop datasetand mine are datasetexpected to be big.
string
dataset
I was wondering if it was faster startsWith indexOf. I experimented with 2,000 entries, but did not find the difference.
startsWith
indexOf
public class Test { public static void main(String args[]) { long value1 = System.currentTimeMillis(); for(long i=0;i<100000000;i++) { "abcd".indexOf("a"); } long value2 = System.currentTimeMillis(); System.out.println(value2-value1); value1 = System.currentTimeMillis(); for(long i=0;i<100000000;i++) { "abcd".startsWith("a"); } value2 = System.currentTimeMillis(); System.out.println(value2-value1); } }
Tested it with this piece of code and perf for startWith seems to be better, for the obvious reason that it does not need to go through a line. But in the best case scenario, the script should run close, while in the worst case scenario, startWith will always work better than indexOf
startsWith - , .
, 2000 ( ). , , , . , 10-30 , , , . ( , , , . - .)
, . indexOf startsWith , indexOf 0. , , . ( , , , -, .)
, -:
", ".
, , .
, . (startsWith, ). , , , . . ; -)
, , startWith, , indexOf. , . (, startsWith indexOf)
, , startsWith , , :
a.startsWith(b) b. , b , .
a.indexOf(b) ( ). a. , , , , .
, , , . .
, , , indexOf .
, indexOf == 0.
, ?
, . , . , . , startWith, , , , , , .
Source: https://habr.com/ru/post/1711552/More articles:memory lock with 64-bit pointers - c ++Visual Studio 2008 - How to disable processing of pending checks - version-controlChanging list contents in Python - python.htaccess 404 page not found - php.net creating broken jpegs in IE and FF - .netiPhone 3.0 SDK ะฟัะพัะธะฒ iPhone 2.0 SDK - objective-cNSXMLParser trims umlauts (รค, รถ, รผ) - xmluseBean tag - javaConvert special characters to PHP - stringEssential Ruby Jewels? - ruby โโ| fooobar.comAll Articles