Java 最快的字节数组连接方法

Java 最快的字节数组连接方法,java,performance,bytearray,concatenation,Java,Performance,Bytearray,Concatenation,我得到了一个以字节数组形式包含消息n部分的映射。在最后一个片段进入地图后,必须连接消息。我找到了两个应该满足要求的解决方案。第一个是使用System.arraycopy: public byte[] getMessageBytes() throws IOException { byte[] bytes = new byte[0]; for (final Map.Entry<Short,byte[]> entry : myMap.entrySet()) {

我得到了一个以字节数组形式包含消息n部分的映射。在最后一个片段进入地图后,必须连接消息。我找到了两个应该满足要求的解决方案。第一个是使用System.arraycopy:

public byte[] getMessageBytes() throws IOException {
    byte[] bytes = new byte[0];
    for (final Map.Entry<Short,byte[]> entry : myMap.entrySet()) {
        byte[] entryBytes = entry.getValue();
        byte[] temp = new byte[bytes.length + entryBytes.length];
        System.arraycopy(bytes, 0, temp, 0, bytes.length);
        System.arraycopy(entryBytes, 0, temp, bytes.length, entryBytes.length);
        bytes = temp;
    }
    return bytes;
}
public byte[]getMessageBytes()引发IOException{
字节[]字节=新字节[0];
对于(最终Map.Entry:myMap.entrySet()){
byte[]entryBytes=entry.getValue();
byte[]temp=新字节[bytes.length+entryBytes.length];
System.arraycopy(字节,0,临时,0,字节.长度);
System.arraycopy(entryBytes,0,temp,bytes.length,entryBytes.length);
字节=温度;
}
返回字节;
}
第二个是使用ByteArrayOutputStream:

public byte[] getMessageBytes() throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    for (final Map.Entry<Short,byte[]> entry : myMap.entrySet()) {
        baos.write(entry.getValue());
    }
    baos.flush();
    return baos.toByteArray();
}
public byte[]getMessageBytes()引发IOException{
最终ByteArrayOutputStream bas=新ByteArrayOutputStream();
对于(最终Map.Entry:myMap.entrySet()){
write(entry.getValue());
}
paos.flush();
返回baos.toByteArray();
}
从性能和内存使用的角度来看,更好的方法是什么?
有没有更好的连接方法?

正确的答案是针对您的特定情况进行测试和比较


这是一个分类地图,像树形地图一样,还是你真的随机合并了字节?

既然你可以通过加上片段的长度来找出消息的大小,我想:

  • 将工件的长度相加,分配输出数组
  • 使用循环将每个部件
    arraycopy()
    放入输出阵列中的正确位置

  • 这可能是内存效率高且速度快的。但是,只有分析才能说明全部情况。

    这应该比您的第一个版本(未测试)性能更好


    (这个答案大致相当于aix的)

    为每次迭代创建一个新数组的第一个解决方案是O(n^2),如果有很多条目,这是一个问题。而且它相当复杂

    使用ByteArrayOutputStream更好有两个原因:它在O(n)中运行,而且非常简单


    最可能快一点的是:如果先计算总大小,然后使用System.arraycopy。但是我只会在ByteArrayOutputStream太慢的情况下这样做。

    首先计算大小并分配一次结果应该是返回字节数组的最快解决方案

    如果稍后将生成的字节数组用作
    InputStream
    ,并且取决于数组的组合大小,最快的方法可能是根本不连接。在这种情况下,您可以通过tearrayinputstreams创建一个包装多个
    。未测试的示例代码:

    Collection<byte[]> values = map.values();
    List<ByteArrayInputStream> streams = new ArrayList<ByteArrayInputStream>(values.size());
    for (byte[] bytes : values) {
        streams.add(new ByteArrayInputStream(bytes));
    }
    return new SequenceInputStream(Collections.enumeration(streams));
    
    Collection values=map.values();
    列表流=新的ArrayList(values.size());
    for(字节[]字节:值){
    add(新的ByteArrayInputStream(字节));
    }
    返回新的SequenceInputStream(Collections.enumeration(streams));
    
    这是本机代码。

        /*
         * public static void arraycopy(Object src, int srcPos, Object dest,
         *      int destPos, int length)
         *
         * The description of this function is long, and describes a multitude
         * of checks and exceptions.
         */
        static void Dalvik_java_lang_System_arraycopy(const u4* args, JValue* pResult)
        {
            ArrayObject* srcArray = (ArrayObject*) args[0];
            int srcPos = args[1];
            ArrayObject* dstArray = (ArrayObject*) args[2];
            int dstPos = args[3];
            int length = args[4];
            /* Check for null pointers. */
            if (srcArray == NULL) {
                dvmThrowNullPointerException("src == null");
                RETURN_VOID();
            }
            if (dstArray == NULL) {
                dvmThrowNullPointerException("dst == null");
                RETURN_VOID();
            }
            /* Make sure source and destination are arrays. */
            if (!dvmIsArray(srcArray)) {
                dvmThrowArrayStoreExceptionNotArray(((Object*)srcArray)->clazz, "source");
                RETURN_VOID();
            }
            if (!dvmIsArray(dstArray)) {
                dvmThrowArrayStoreExceptionNotArray(((Object*)dstArray)->clazz, "destination");
                RETURN_VOID();
            }
            /* avoid int overflow */
            if (srcPos < 0 || dstPos < 0 || length < 0 ||
                srcPos > (int) srcArray->length - length ||
                dstPos > (int) dstArray->length - length)
            {
                dvmThrowExceptionFmt(gDvm.exArrayIndexOutOfBoundsException,
                    "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
                    srcArray->length, srcPos, dstArray->length, dstPos, length);
                RETURN_VOID();
            }
            ClassObject* srcClass = srcArray->clazz;
            ClassObject* dstClass = dstArray->clazz;
            char srcType = srcClass->descriptor[1];
            char dstType = dstClass->descriptor[1];
            /*
             * If one of the arrays holds a primitive type, the other array must
             * hold the same type.
             */
            bool srcPrim = (srcType != '[' && srcType != 'L');
            bool dstPrim = (dstType != '[' && dstType != 'L');
            if (srcPrim || dstPrim) {
                if (srcPrim != dstPrim || srcType != dstType) {
                    dvmThrowArrayStoreExceptionIncompatibleArrays(srcClass, dstClass);
                    RETURN_VOID();
                }
                if (false) ALOGD("arraycopy prim[%c] dst=%p %d src=%p %d len=%d",
                    srcType, dstArray->contents, dstPos,
                    srcArray->contents, srcPos, length);
                switch (srcType) {
                case 'B':
                case 'Z':
                    /* 1 byte per element */
                    memmove((u1*) dstArray->contents + dstPos,
                        (const u1*) srcArray->contents + srcPos,
                        length);
                    break;
                case 'C':
                case 'S':
                    /* 2 bytes per element */
                    move16((u1*) dstArray->contents + dstPos * 2,
                        (const u1*) srcArray->contents + srcPos * 2,
                        length * 2);
                    break;
                case 'F':
                case 'I':
                    /* 4 bytes per element */
                    move32((u1*) dstArray->contents + dstPos * 4,
                        (const u1*) srcArray->contents + srcPos * 4,
                        length * 4);
                    break;
                case 'D':
                case 'J':
                    /*
                     * 8 bytes per element.  We don't need to guarantee atomicity
                     * of the entire 64-bit word, so we can use the 32-bit copier.
                     */
                    move32((u1*) dstArray->contents + dstPos * 8,
                        (const u1*) srcArray->contents + srcPos * 8,
                        length * 8);
                    break;
                default:        /* illegal array type */
                    ALOGE("Weird array type '%s'", srcClass->descriptor);
                    dvmAbort();
                }
            } else {
                /*
                 * Neither class is primitive.  See if elements in "src" are instances
                 * of elements in "dst" (e.g. copy String to String or String to
                 * Object).
                 */
                const int width = sizeof(Object*);
                if (srcClass->arrayDim == dstClass->arrayDim &&
                    dvmInstanceof(srcClass, dstClass))
                {
                    /*
                     * "dst" can hold "src"; copy the whole thing.
                     */
                    if (false) ALOGD("arraycopy ref dst=%p %d src=%p %d len=%d",
                        dstArray->contents, dstPos * width,
                        srcArray->contents, srcPos * width,
                        length * width);
                    move32((u1*)dstArray->contents + dstPos * width,
                        (const u1*)srcArray->contents + srcPos * width,
                        length * width);
                    dvmWriteBarrierArray(dstArray, dstPos, dstPos+length);
                } else {
                    /*
                     * The arrays are not fundamentally compatible.  However, we
                     * may still be able to do this if the destination object is
                     * compatible (e.g. copy Object[] to String[], but the Object
                     * being copied is actually a String).  We need to copy elements
                     * one by one until something goes wrong.
                     *
                     * Because of overlapping moves, what we really want to do
                     * is compare the types and count up how many we can move,
                     * then call move32() to shift the actual data.  If we just
                     * start from the front we could do a smear rather than a move.
                     */
                    Object** srcObj;
                    int copyCount;
                    ClassObject*   clazz = NULL;
                    srcObj = ((Object**)(void*)srcArray->contents) + srcPos;
                    if (length > 0 && srcObj[0] != NULL)
                    {
                        clazz = srcObj[0]->clazz;
                        if (!dvmCanPutArrayElement(clazz, dstClass))
                            clazz = NULL;
                    }
                    for (copyCount = 0; copyCount < length; copyCount++)
                    {
                        if (srcObj[copyCount] != NULL &&
                            srcObj[copyCount]->clazz != clazz &&
                            !dvmCanPutArrayElement(srcObj[copyCount]->clazz, dstClass))
                        {
                            /* can't put this element into the array */
                            break;
                        }
                    }
                    if (false) ALOGD("arraycopy iref dst=%p %d src=%p %d count=%d of %d",
                        dstArray->contents, dstPos * width,
                        srcArray->contents, srcPos * width,
                        copyCount, length);
                    move32((u1*)dstArray->contents + dstPos * width,
                        (const u1*)srcArray->contents + srcPos * width,
                        copyCount * width);
                    dvmWriteBarrierArray(dstArray, 0, copyCount);
                    if (copyCount != length) {
                        dvmThrowArrayStoreExceptionIncompatibleArrayElement(srcPos + copyCount,
                                srcObj[copyCount]->clazz, dstClass);
                        RETURN_VOID();
                    }
                }
            }
            RETURN_VOID();
        }
    
    /*
    *公共静态void数组副本(对象src、int-srcPos、对象dest、,
    *int destPos,int length)
    *
    *此函数的描述很长,并且描述了大量
    *检查和例外情况。
    */
    静态无效Dalvik_java_lang_System_arraycopy(常量u4*args,JValue*pResult)
    {
    ArrayObject*srcArray=(ArrayObject*)参数[0];
    int srcPos=args[1];
    ArrayObject*dstArray=(ArrayObject*)参数[2];
    int-dstPos=args[3];
    int length=args[4];
    /*检查空指针*/
    if(srcArray==NULL){
    dvmThrowNullPointerException(“src==null”);
    RETURN_VOID();
    }
    if(dstArray==NULL){
    dvmThrowNullPointerException(“dst==null”);
    RETURN_VOID();
    }
    /*确保源和目标是阵列*/
    if(!dvmIsArray(srcArray)){
    dvmThrowArrayStoreExceptionNotArray(((Object*)srcArray)->clazz,“source”);
    RETURN_VOID();
    }
    if(!dvmIsArray(dstArray)){
    dvmThrowArrayStoreExceptionNotArray(((Object*)dstArray)->clazz,“destination”);
    RETURN_VOID();
    }
    /*避免int溢出*/
    如果(srcPos<0 | | dstPos<0 | |长度<0||
    srcPos>(int)srcArray->length-length||
    dstPos>(int)DSTARY->长度-长度)
    {
    dvmThrowExceptionFmt(gDvm.exArrayIndexOutOfBoundsException,
    “src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d”,
    srcArray->length,srcPos,dstArray->length,dstPos,length);
    RETURN_VOID();
    }
    ClassObject*srcClass=srcArray->clazz;
    ClassObject*dstClass=dstArray->clazz;
    char srcType=srcClass->descriptor[1];
    char dstType=dstClass->descriptor[1];
    /*
    *如果其中一个数组包含基元类型,则另一个数组必须
    *保持相同的类型。
    */
    bool srcPrim=(srcType!='['&&srcType!='L');
    bool dstPrim=(dstType!='['&&dstType!='L');
    if(srcPrim | dstPrim){
    if(srcPrim!=dstPrim | | srcType!=dstpype){
    DVMTHROWARRAYSTOREExceptionInCompatibleLearRays(srcClass,dstClass);
    RETURN_VOID();
    }
    如果(false)ALOGD(“阵列复制原语[%c]dst=%p%d src=%p%d len=%d”,
    srcType,dstArray->contents,dstPos,
    srcArray->contents,srcPos,length);
    开关(SRC型){
    案例“B”:
    案例“Z”:
    /*每个元素1字节*/
    memmove((u1*)DSTARRY->contents+dstPos,
    (const u1*)srcArray->contents+srcPos,
    长度);
    打破
    案例“C”:
    案例S:
    /*每个元素2字节*/
    
        /*
         * public static void arraycopy(Object src, int srcPos, Object dest,
         *      int destPos, int length)
         *
         * The description of this function is long, and describes a multitude
         * of checks and exceptions.
         */
        static void Dalvik_java_lang_System_arraycopy(const u4* args, JValue* pResult)
        {
            ArrayObject* srcArray = (ArrayObject*) args[0];
            int srcPos = args[1];
            ArrayObject* dstArray = (ArrayObject*) args[2];
            int dstPos = args[3];
            int length = args[4];
            /* Check for null pointers. */
            if (srcArray == NULL) {
                dvmThrowNullPointerException("src == null");
                RETURN_VOID();
            }
            if (dstArray == NULL) {
                dvmThrowNullPointerException("dst == null");
                RETURN_VOID();
            }
            /* Make sure source and destination are arrays. */
            if (!dvmIsArray(srcArray)) {
                dvmThrowArrayStoreExceptionNotArray(((Object*)srcArray)->clazz, "source");
                RETURN_VOID();
            }
            if (!dvmIsArray(dstArray)) {
                dvmThrowArrayStoreExceptionNotArray(((Object*)dstArray)->clazz, "destination");
                RETURN_VOID();
            }
            /* avoid int overflow */
            if (srcPos < 0 || dstPos < 0 || length < 0 ||
                srcPos > (int) srcArray->length - length ||
                dstPos > (int) dstArray->length - length)
            {
                dvmThrowExceptionFmt(gDvm.exArrayIndexOutOfBoundsException,
                    "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
                    srcArray->length, srcPos, dstArray->length, dstPos, length);
                RETURN_VOID();
            }
            ClassObject* srcClass = srcArray->clazz;
            ClassObject* dstClass = dstArray->clazz;
            char srcType = srcClass->descriptor[1];
            char dstType = dstClass->descriptor[1];
            /*
             * If one of the arrays holds a primitive type, the other array must
             * hold the same type.
             */
            bool srcPrim = (srcType != '[' && srcType != 'L');
            bool dstPrim = (dstType != '[' && dstType != 'L');
            if (srcPrim || dstPrim) {
                if (srcPrim != dstPrim || srcType != dstType) {
                    dvmThrowArrayStoreExceptionIncompatibleArrays(srcClass, dstClass);
                    RETURN_VOID();
                }
                if (false) ALOGD("arraycopy prim[%c] dst=%p %d src=%p %d len=%d",
                    srcType, dstArray->contents, dstPos,
                    srcArray->contents, srcPos, length);
                switch (srcType) {
                case 'B':
                case 'Z':
                    /* 1 byte per element */
                    memmove((u1*) dstArray->contents + dstPos,
                        (const u1*) srcArray->contents + srcPos,
                        length);
                    break;
                case 'C':
                case 'S':
                    /* 2 bytes per element */
                    move16((u1*) dstArray->contents + dstPos * 2,
                        (const u1*) srcArray->contents + srcPos * 2,
                        length * 2);
                    break;
                case 'F':
                case 'I':
                    /* 4 bytes per element */
                    move32((u1*) dstArray->contents + dstPos * 4,
                        (const u1*) srcArray->contents + srcPos * 4,
                        length * 4);
                    break;
                case 'D':
                case 'J':
                    /*
                     * 8 bytes per element.  We don't need to guarantee atomicity
                     * of the entire 64-bit word, so we can use the 32-bit copier.
                     */
                    move32((u1*) dstArray->contents + dstPos * 8,
                        (const u1*) srcArray->contents + srcPos * 8,
                        length * 8);
                    break;
                default:        /* illegal array type */
                    ALOGE("Weird array type '%s'", srcClass->descriptor);
                    dvmAbort();
                }
            } else {
                /*
                 * Neither class is primitive.  See if elements in "src" are instances
                 * of elements in "dst" (e.g. copy String to String or String to
                 * Object).
                 */
                const int width = sizeof(Object*);
                if (srcClass->arrayDim == dstClass->arrayDim &&
                    dvmInstanceof(srcClass, dstClass))
                {
                    /*
                     * "dst" can hold "src"; copy the whole thing.
                     */
                    if (false) ALOGD("arraycopy ref dst=%p %d src=%p %d len=%d",
                        dstArray->contents, dstPos * width,
                        srcArray->contents, srcPos * width,
                        length * width);
                    move32((u1*)dstArray->contents + dstPos * width,
                        (const u1*)srcArray->contents + srcPos * width,
                        length * width);
                    dvmWriteBarrierArray(dstArray, dstPos, dstPos+length);
                } else {
                    /*
                     * The arrays are not fundamentally compatible.  However, we
                     * may still be able to do this if the destination object is
                     * compatible (e.g. copy Object[] to String[], but the Object
                     * being copied is actually a String).  We need to copy elements
                     * one by one until something goes wrong.
                     *
                     * Because of overlapping moves, what we really want to do
                     * is compare the types and count up how many we can move,
                     * then call move32() to shift the actual data.  If we just
                     * start from the front we could do a smear rather than a move.
                     */
                    Object** srcObj;
                    int copyCount;
                    ClassObject*   clazz = NULL;
                    srcObj = ((Object**)(void*)srcArray->contents) + srcPos;
                    if (length > 0 && srcObj[0] != NULL)
                    {
                        clazz = srcObj[0]->clazz;
                        if (!dvmCanPutArrayElement(clazz, dstClass))
                            clazz = NULL;
                    }
                    for (copyCount = 0; copyCount < length; copyCount++)
                    {
                        if (srcObj[copyCount] != NULL &&
                            srcObj[copyCount]->clazz != clazz &&
                            !dvmCanPutArrayElement(srcObj[copyCount]->clazz, dstClass))
                        {
                            /* can't put this element into the array */
                            break;
                        }
                    }
                    if (false) ALOGD("arraycopy iref dst=%p %d src=%p %d count=%d of %d",
                        dstArray->contents, dstPos * width,
                        srcArray->contents, srcPos * width,
                        copyCount, length);
                    move32((u1*)dstArray->contents + dstPos * width,
                        (const u1*)srcArray->contents + srcPos * width,
                        copyCount * width);
                    dvmWriteBarrierArray(dstArray, 0, copyCount);
                    if (copyCount != length) {
                        dvmThrowArrayStoreExceptionIncompatibleArrayElement(srcPos + copyCount,
                                srcObj[copyCount]->clazz, dstClass);
                        RETURN_VOID();
                    }
                }
            }
            RETURN_VOID();
        }