重新存储已经存储在目标变量中的一个值是否会导致重写并延长运行时间?

我真正担心的是,如果我编写了一个导致将值分配给变量的表达式,该变量已经存储了要分配的值。

例如:

import React from 'react';
import { connect } from 'react-redux';
import { reduxForm,FieldArray,Form } from 'redux-form';
import { Button,Container,Row,Col } from 'reactstrap';
import _map from 'lodash/map';
import ReactJson from 'react-json-view';

import { WEEK_DAYS } from '../common/constants';
import {
  clearReservations,saveReservations,} from '../actions/machine';
import SingleDayReservations from './SingleDayReservations';
import './Reservations.scss';

const validate = values => {
  const errors = {
    // monday: [{ start: 'must be present' }],//tuesday: { _error: 'error' },};
  return errors;
};

const Reservations = ({
  clearReservations,handleSubmit,machine,}) => (
  <Container classname="reservations">
    <Form onSubmit={handleSubmit(saveReservations)}>
      <Row>
        <Col xs={8}>
          <h2>Reservations</h2>
          {_map(WEEK_DAYS,day => (
            <FieldArray
              key={`single-${day}`}
              component={SingleDayReservations}
              name={day}
            />
          ))}
          <Button color="primary" type="submit">
            Save data
          </Button>
        </Col>
        <Col xs={4}>
          <ReactJson src={machine} name="machinestoreState" />
          <Button
            onClick={clearReservations}
            color="warning"
            classname="reservations__clear-btn"
          >
            Reset Data
          </Button>
        </Col>
      </Row>
    </Form>
  </Container>
);

const mapstatetoProps = state => ({
  machine: state.machine,initialValues: state.machine,});

const mapDispatchToProps = {
  clearReservations,};

export default connect(
  mapstatetoProps,mapDispatchToProps,)(
  reduxForm({
    form: 'reservations',validate,enableReinitialize: true,})(Reservations),);

它是否将#include <stdio.h> int main(void) { int var = 1; printf("The actual value of var is %d",var); var = 1; // What happens exactly if I bring in this expression? // Does it rewrite the memory? return 0; } 的值重写为存储器中的1,这会导致更长的运行时间吗?

还是看起来只是跳过了分配命令?


我一直在寻找确切的答案,但我找不到此处已经提出的内部问题,而且在我看来也找不到C99。

这个问题是针对C和C ++的,因为我都使用C和C ++,所以我不想两次提出相同的问题。如果在这两种选择之间都能找到答案,请说明重点关注哪种语言。

sunyylove 回答:重新存储已经存储在目标变量中的一个值是否会导致重写并延长运行时间?

让我们尝试here

enter image description here

如您所见,此编译器将重新分配该值。 var = 1;语句转换为mov指令。现在,让我们尝试使用更高的优化级别:

enter image description here

现在var = 1;不会转换为任何程序集。它已经过优化。甚至int var = 1;都已被优化,现在1的值已为该printf调用进行了硬编码。

通常,它取决于编译器,选项,语言以及可能的许多其他内容。如今,现代的编译器通常会优化此类代码,但是如果您想确定的话,应该始终自己尝试一下。

本文链接:https://www.f2er.com/3029378.html

大家都在问