2013-12-19 1 views
2

스칼라의 foreach를 사용하여 CQEngine의 ResultSet을 처리하려고하는데 결과가 매우 느립니다. 다음스칼라를 사용하는 CQEngine 결과 집합 foreach가 매우 느립니다.

어떻게 든 .foreach 방법은 매우 느립니다 내가

import collection.JavaConversions._ 
val query = existIn(myOtherCollection, REFERENCE, REFERENCE) 
val resultSet = myIndexCollection.retrieve(query) 
resultSet.foreach(r =>{ 
    //do something here 
}) 

을 할 노력하고있어의 조각입니다. 나는 SimonMonitor를 넣고 디버그 하려다가 while(resultSet.hasNext)을 사용하여 .foreach를 변경했다. 놀랍게도 hasNext 메서드를 호출 할 때마다 약 1-2 초가 걸린다. 매우 느립니다.

Java를 사용하여 동일한 버전을 만들려고했는데 Java 버전이 매우 빠릅니다.

도와주세요

+0

더 많은 코드를 게시 할 수 있습니까? Java에서 구현되는 직접적인'hasNext'를 사용하는 것이 스칼라 코드 기반에서 호출하기 때문에 왜 느려지는지 이해할 수 없습니다. 더 많은 일이 일어나야합니다. – wheaties

+0

@wheaties 그게 내가 생각하고 있던 것이지만, 내 실수가있는 곳을 찾지 못했습니다. 나는 사무실에 들어서 더 많은 코드를 게시 할 것이다. – Wins

+0

확인,이 질문을 확인하는 것이 좋습니다. – wheaties

답변

2

아래의 테스트 코드로 문제를 재현 할 수 없습니다. 당신은 당신의 시스템에서 그것을 시험해보고 그것을 어떻게 실행하는지 알려 줄 수 있습니까?

(BOTH 스칼라와 자바 반복자는 엄청나게 빠르게 실행할 수 있도록 주석을 해제 라인 (38), garages.addIndex(HashIndex.onAttribute(Garage.BRANDS_SERVICED)), ...) 처음

출력 (밀리 초 시간) : 아래

Done adding data 
Done adding index 
============== Scala ============== 
Car{carId=4, name='BMW M3', description='2013 model', features=[radio, convertible]} 
Time : 3 seconds 
Car{carId=1, name='Ford Focus', description='great condition, low mileage', features=[spare tyre, sunroof]} 
Time : 1 seconds 
Car{carId=2, name='Ford Taurus', description='dirty and unreliable, flat tyre', features=[spare tyre, radio]} 
Time : 2 seconds 
============== Java ============== 
Car{carId=4, name='BMW M3', description='2013 model', features=[radio, convertible]} 
Time : 3 seconds 
Car{carId=1, name='Ford Focus', description='great condition, low mileage', features=[spare tyre, sunroof]} 
Time : 1 seconds 
Car{carId=2, name='Ford Taurus', description='dirty and unreliable, flat tyre', features=[spare tyre, radio]} 
Time : 2 seconds 

코드 :

import collection.JavaConversions._ 
import com.googlecode.cqengine.query.QueryFactory._ 
import com.googlecode.cqengine.CQEngine; 
import com.googlecode.cqengine.index.hash._; 
import com.googlecode.cqengine.IndexedCollection; 
import com.googlecode.cqengine.query.Query; 
import java.util.Arrays.asList; 

object CQTest { 

    def main(args: Array[String]) { 

    val cars: IndexedCollection[Car] = CQEngine.newInstance(); 
    cars.add(new Car(1, "Ford Focus", "great condition, low mileage", asList("spare tyre", "sunroof"))); 
    cars.add(new Car(2, "Ford Taurus", "dirty and unreliable, flat tyre", asList("spare tyre", "radio"))); 
    cars.add(new Car(3, "Honda Civic", "has a flat tyre and high mileage", asList("radio"))); 
    cars.add(new Car(4, "BMW M3", "2013 model", asList("radio", "convertible"))); 

    // add cruft to try and slow down CQE 
    for (i <- 1 to 10000) { 
     cars.add(new Car(i, "BMW2014_" + i, "2014 model", asList("radio", "convertible"))) 
    } 

    // Create an indexed collection of garages... 
    val garages: IndexedCollection[Garage] = CQEngine.newInstance(); 
    garages.add(new Garage(1, "Joe's garage", "London", asList("Ford Focus", "Honda Civic"))); 
    garages.add(new Garage(2, "Jane's garage", "Dublin", asList("BMW M3"))); 
    garages.add(new Garage(3, "John's garage", "Dublin", asList("Ford Focus", "Ford Taurus"))); 
    garages.add(new Garage(4, "Jill's garage", "Dublin", asList("Ford Focus"))); 

    // add cruft to try and slow down CQE 
    for (i <- 1 to 10000) { 
     garages.add(new Garage(i, "Jill's garage", "Dublin", asList("DONT_MATCH_CARS_BMW2014_" + i))) 
    } 

    println("Done adding data") 
    // cars.addIndex(HashIndex.onAttribute(Car.NAME)); 
    // garages.addIndex(HashIndex.onAttribute(Garage.BRANDS_SERVICED)); 
    println("Done adding index") 
    val query = existsIn(garages, Car.NAME, Garage.BRANDS_SERVICED, equal(Garage.LOCATION, "Dublin")) 
    val resultSet = cars.retrieve(query) 

    var previous = System.currentTimeMillis() 
    println("============== Scala ============== ") 
    // Scala version 
    resultSet.foreach(r => { 
     println(r); 
     val t = (System.currentTimeMillis() - previous) 
     System.out.println("Time : " + t/1000 + " seconds") 
     previous = System.currentTimeMillis() 
    }) 

    println("============== Java ============== ") 
    previous = System.currentTimeMillis() 
    // Java version 
    val i: java.util.Iterator[Car] = resultSet.iterator() 
    while (i.hasNext) { 
     val r = i.next() 
     println(r); 
     val t = (System.currentTimeMillis() - previous) 
     System.out.println("Time : " + t/1000 + " seconds") 
     previous = System.currentTimeMillis() 
    } 
    } 
} 
+0

2 IndexCollection에서 쿼리하려고한다는 것을 잊어 버렸습니다. 따라서 existsIn은 컬렉션 1의 자동차를 쿼리하는 데 사용됩니다.이 컬렉션의 컬렉션 2에는 Car.NAME이라는 속성이 있습니다. 내일 사무실에 도착하면 더 많은 코드를 게시하려고합니다. – Wins

+1

a) 기존의 기반 조인을 수행합니다. b) cruft를 많이 추가합니다. + CQEngine이 시간을 많이 소비하도록 인덱스를 추가하지 않습니다. 이 경우 Scala와 Java 모두 조회 당 1 ~ 3 초가 걸리는 것으로 보입니다. 인덱스를 추가하면, garala.addIndex (HashIndex.onAttribute (Garage.BRANDS_SERVICED))'38 행은 스칼라와 자바 모두 쿼리를 즉각적으로 처리합니다 ... 시스템에서 이것을 시도하고 그것이 코드에 대해 다른 점에 대한 통찰력 ...? – vijucat

+0

업데이트 된 코드를 보내 주셔서 감사합니다. 내 코드를 시뮬레이트 할 수있는 3 가지가 더 있습니다. 1. 일치하는 필드는 두 컬렉션의 개체에서 5 문자의 영숫자 (대문자이므로 쉽게 만들 수 있음)를 사용하여 무작위로 생성됩니다. 2. IndexCollection에서 1 백만 개의 개체를 제출하십시오. 3. existIn 쿼리를 existIn (garages, Car.NAME, Garage.BRANDS_SERVICED)로 바꾸십시오. 단지 3 개의 매개 변수 만 사용하십시오. 나는. 네 번째 매개 변수를 제거하십시오. 그건 내 코드를 실제로 시뮬레이트 할거야. – Wins

관련 문제