예시 값
private val smaple = "body\n" +
"{\n" +
"margin: 0px !important;\n" +
"padding: 0px !important;\n" +
"}\n" +
"header\n" +
"{\n" +
// "background-image: url(\"https://iso.500px.com/wp-content/uploads/2015/12/stock-photo-125301449-1500x822.jpg\");\n" +
"background-postion: center center;\n" +
"background-position-y: 30%;\n" +
"overflow: hidden;\n" +
"height: 200px;\n" +
"width: 100%;\n" +
"text-align: center;\n" +
"}\n" +
"\n" +
"header h1 {\n" +
"color: #F0477F;\n" +
"font-family: \"Roboto\", sans-serif;\n" +
"font-size: 1.999em;\n" +
"font-weight: 400;\n" +
"}\n" +
"\n" +
"header h2 {\n" +
"\n" +
"color: #F0477F;\n" +
"font-family: \"Roboto\", sans-serif;\n" +
"font-size: 1.414em;\n" +
"font-weight: 400;\n" +
"\n" +
"}"+
".asdlkah\n" +
"{\n" +
// "background-image: url(\"https://iso.500px.com/wp-content/uploads/2015/12/stock-photo-125301449-1500x822.jpg\");\n" +
"background-postion: center center;\n" +
"background-position-y: 30%;\n" +
"overflow: hidden;\n" +
"height: 200px;\n" +
"width: 100%;\n" +
"text-align: center;\n" +
"}\n" +
".amzxcnzxmc\n" +
"{\n" +
// "background-image: url(\"https://iso.500px.com/wp-content/uploads/2015/12/stock-photo-125301449-1500x822.jpg\");\n" +
"background-postion: center center;\n" +
"background-position-y: 30%;\n" +
"overflow: hidden;\n" +
"height: 200px;\n" +
"width: 100%;\n" +
"text-align: center;\n" +
"}\n"
값 비교
//indexOf
private fun test1(){
val target = "background-image:"
val start = System.nanoTime()
val doc = Jsoup.parse("<HTML><STYLE>${smaple}</STYLE></HTML>")
val index = doc.head().select("style").first()?.run { this.data().indexOf(target) }
logger.info("speed: ${System.nanoTime() - start} idx: ${index}")
}
//contains
private fun test2(){
val target = "background-image:"
val start = System.nanoTime()
val doc = Jsoup.parse("<HTML><STYLE>${smaple}</STYLE></HTML>")
val index = doc.head().select("style").first()?.run { this.data().contains(target, ignoreCase = true) }
logger.info("speed: ${System.nanoTime() - start} idx: ${index}")
}
//contains
private fun test3(){
val target = "background-image:"
val start = System.nanoTime()
val doc = Jsoup.parse("<HTML><STYLE>${smaple}</STYLE></HTML>")
val has = doc.head().select("style").first()?.run { this.data().contains(target, ignoreCase = false) }
logger.info("speed: ${System.nanoTime() - start} has: ${has}")
}
//contains
private fun test4(){
val target = "background-image:"
val start = System.nanoTime()
val doc = Jsoup.parse("<HTML><STYLE>${smaple}</STYLE></HTML>")
val has = doc.head().select("style").first()?.run { this.data().matches(target.toRegex()) }
logger.info("speed: ${System.nanoTime() - start} has: ${has}")
}
결과
1. 결과값을 찾을 수 없는 경우
speed: 52209000 idx: -1
speed: 501400 idx: false
speed: 222200 has: false
speed: 332600 has: false
2. 결과값을 찾을 수 있는 경우
speed: 47471500 idx: 563
speed: 278100 idx: true
speed: 149200 has: true
speed: 243500 has: false
결론
contains가 빠르다