2016-04-05 13 views
0

今日、私はkotlinのnum.inc()機能について学び、それを自分のコードに実装することに決めました。言うまでもなく、それは私のコードに10倍の待ち時間を追加しました(4000 400msの〜から行ってきました+ MS)ここで Kotlin i.inc()はi ++よりも10倍遅いですか?

は私の伝統的な方法での例です(私は++、400msで)ここにある

package com.beaudoin 

import java.io.BufferedWriter 
import java.io.FileInputStream 
import java.io.FileWriter 
import java.nio.channels.FileChannel 

fun main(args: Array<String>) { 
    val s = System.currentTimeMillis() 

    val channel = FileInputStream("client.dll").channel 
    val buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()) 
    val data = ByteArray(buffer.capacity()) 
    buffer.get(data) 

    val writer = BufferedWriter(FileWriter("dump.txt", false)) 
    val bytes = ByteArray(16) 

    var offset = 0 
    var i = 0 
    while (i < data.size) { 
     for (j in bytes.indices) { 
      bytes[j] = data[i++] 
     } 
     writer.write(HexRow(offset, bytes).toString()) 
     writer.newLine() 
     offset += 16 
    } 

    writer.close() 

    println(System.currentTimeMillis() - s) 
} 

private val HEX_ARRAY = "ABCDEF".toCharArray() 
private val bytes = ByteArray(4); 

class HexRow(val offset: Int, val values: ByteArray) { 

    fun bytesToChar(bytes: ByteArray, width: Int): CharArray { 
     val hexChars = CharArray((bytes.size * 2) + (bytes.size/width)) 

     for (i in bytes.indices) { 
      val v = bytes[i].toInt() and 0xFF 
      val idx = (i * 2) + i/width 

      hexChars[idx] = HEX_ARRAY[v.ushr(4)] 
      hexChars[idx + 1] = HEX_ARRAY[v and 0x0F] 
      if (idx + 2 < hexChars.size) { 
       hexChars[idx + 2] = ' ' 
      } 
     } 
     return hexChars; 
    } 

    fun bytesToHex(value: Int) = String(bytesToChar(toByteArray(value), 6)) 

    fun bytesToHex(bytes: ByteArray) = String(bytesToChar(bytes, 1)) 

    fun toByteArray(value: Int): ByteArray { 
     bytes[0] = value.ushr(24).toByte() 
     bytes[1] = value.ushr(16).toByte() 
     bytes[2] = value.ushr(8).toByte() 
     bytes[3] = value.toByte() 
     return bytes 
    } 

    override fun toString() = bytesToHex(offset) + " " + bytesToHex(values) 

} 

とコード使用i.inc()(4000ms +)

package com.beaudoin 

import java.io.BufferedWriter 
import java.io.FileInputStream 
import java.io.FileWriter 
import java.nio.channels.FileChannel 

fun main(args: Array<String>) { 
    val s = System.currentTimeMillis() 

    val channel = FileInputStream("client.dll").channel 
    val buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()) 
    val data = ByteArray(buffer.capacity()) 
    buffer.get(data) 

    val writer = BufferedWriter(FileWriter("dump.txt", false)) 
    val bytes = ByteArray(16) 

    var offset = 0 
/* var i = 0 
    while (i < data.size) { 
     for (j in bytes.indices) { 
      bytes[j] = data[i++] 
     } 
     writer.write(HexRow(offset, bytes).toString()) 
     writer.newLine() 
     offset += 16 
    }*/ 

    for (i in data.indices) { 
     for (j in bytes.indices) { 
      bytes[j] = data[i] 
      i.inc() 
     } 
     writer.write(HexRow(offset, bytes).toString()) 
     writer.newLine() 
     offset += 16 
    } 

    writer.close() 

    println(System.currentTimeMillis() - s) 
} 

private val HEX_ARRAY = "ABCDEF".toCharArray() 
private val bytes = ByteArray(4); 

class HexRow(val offset: Int, val values: ByteArray) { 

    fun bytesToChar(bytes: ByteArray, width: Int): CharArray { 
     val hexChars = CharArray((bytes.size * 2) + (bytes.size/width)) 

     for (i in bytes.indices) { 
      val v = bytes[i].toInt() and 0xFF 
      val idx = (i * 2) + i/width 

      hexChars[idx] = HEX_ARRAY[v.ushr(4)] 
      hexChars[idx + 1] = HEX_ARRAY[v and 0x0F] 
      if (idx + 2 < hexChars.size) { 
       hexChars[idx + 2] = ' ' 
      } 
     } 
     return hexChars; 
    } 

    fun bytesToHex(value: Int) = String(bytesToChar(toByteArray(value), 6)) 

    fun bytesToHex(bytes: ByteArray) = String(bytesToChar(bytes, 1)) 

    fun toByteArray(value: Int): ByteArray { 
     bytes[0] = value.ushr(24).toByte() 
     bytes[1] = value.ushr(16).toByte() 
     bytes[2] = value.ushr(8).toByte() 
     bytes[3] = value.toByte() 
     return bytes 
    } 

    override fun toString() = bytesToHex(offset) + " " + bytesToHex(values) 

} 

誰かがi.incは()そんなに遅く、私は++よりも、ある理由を教えていただけますか?

PS:でなければなりません(任意のファイルでclient.dllを交換してください〜あなたのコード内で、ここで鉱山への正確な数字を取得したり、単に私のテストファイルをダウンロードするための12メガバイトhere

答えて

7

inc()コールが完全に冗長である:それは変更されません。変数:実行時間の差については

var x = 0 
x.inc() 
println(x) // 0 

は、2つの実装であっても意味は異なります。

  • まずスニペット:

    while (i < data.size) { 
        for (j in bytes.indices) { 
         bytes[j] = data[i++] 
        } 
        //... 
    } 
    

    内部ループがiを変えるので、各iためwhileの繰り返しがないだろう、iの一部(も、最も、私は仮定)の値がスキップされます。

  • 第二スニペット:

    for (i in data.indices) { 
        for (j in bytes.indices) { 
         bytes[j] = data[i] // removed redundant i.inc() 
        } 
        //... 
    } 
    

    内部ループは、したがって、それは非常に長い時間がかかりますが、外側のループで各iのために呼ばれています。

+0

ああ、それをキャッチしませんでした。ありがとう! –

関連する問題