非平凡的goto使用(可能击败编译器)

时间:2017-11-11 16:48:18

标签: c optimization goto

我有一段像这样的代码:

f += .001f; //Only needs to be executed when loop executes at least one iteration, but does no harm if incremented without entering the loop
while(anIndex < aVaryingBoundary) {
    if(something) {
        //code
        continue;
    }
    //more code
}

我发现使这段代码更有效率的唯一方法是(通过消除不必要的f增量)是使用goto。

if(anIndex < aVaryingBoundary) {
    f += .001f;

loop:
    if(something) {
        //code
        if(anIndex < aVaryingBoundary) {
            goto loop;
        }
        else {
            goto loop_end;
        }
    }
    //more code
    if(anIndex < aVaryingBoundary) {
            goto loop;
    }
}
loop_end:

即使这是一个简单的优化,我也不认为编译器可以轻松检测到这一点。编译器执行它真的不重要吗?

3 个答案:

答案 0 :(得分:3)

不仅仅是

if (anIndex < aVaryingBoundary) {
    f += .001f;
    do {
        if(something) {
            //code
            continue;
        }
        //more code
    } while(anIndex < aVaryingBoundary);
}

答案 1 :(得分:2)

通过这种方式,您不需要'use strict'; console.log('Loading function'); var doc = require('dynamodb-doc'); var dynamodb = new doc.DynamoDB(); exports.handler = (event, context, callback) => { var batch = event.datasetRecords.batch.newValue; var datetime = new Date().getTime(); var putList = []; function putError(err) { console.log('Error inserting: ' + err); throw `Error putting item into DB: ${err}.`; } function doDelete(err, data) { if (err) { console.log('Error deleting: ' + err); throw `Error deleting item: ${err}.`; }} function doQuery(err, data) { if (err) { console.log("Error querying: " + err); throw `Error querying submission: ${err}`; } else { data.Items.forEach(function(item) { Object.assign(item, { "uid" : event.identityId, "date": datetime }); var putRequest = {"TableName" : "StoryEntity", "Item" : item, "ConditionExpression" : "attribute_not_exists(#u) or (#u=:user)", "ExpressionAttributeNames" : {"#u":"uid"}, "ExpressionAttributeValues" : {":user":event.identityId} }; var promise = dynamodb.putItem(putRequest).promise(); putList.push({"promise":promise, "entityId":item.EntityId}); }); var promiseList = []; putList.forEach(function(item){ promiseList.push(item.promise.then(function() { dynamodb.deleteItem({"TableName" : "Submission", "Key" : {"Batch" : batch, "EntityId": item.entityId }}, doDelete); }, putError)); }); Promise.all(promiseList).then(function() { console.log("Success!"); callback(null, event); }, function(err) { console.log(err); callback(err); }); } } dynamodb.query({ TableName: "Submission", ProjectionExpression: "EntityId, #t, EntityType, #n, Container, Destination, Target", KeyConditionExpression: "#b = :batch", ExpressionAttributeNames: { "#b" : "Batch", "#t" : "Text", "#n" : "Name" }, ExpressionAttributeValues: { ":batch": batch } }, doQuery); }; ,编译器可以对其进行优化。

goto

主要逻辑结构保持不变,但不再有if(anIndex < aVaryingBoundary) { f += .001f; // Tag loop: while (true) { if(something) { //code if(anIndex < aVaryingBoundary) { continue; } else { break; } } //more code if(anIndex < aVaryingBoundary) { continue; } break; } } // Tag loop_end: s。

答案 2 :(得分:0)

ionic cordova plugin rm cordova-plugin-compat --force
ionic cordova plugin add cordova-plugin-compat@1.2