创建循环的高速缓存有效版本

时间:2019-04-10 19:28:51

标签: c loops caching

我正在尝试创建嵌套循环的缓存有效版本。循环的长度应使与循环关联的数据大于缓存的大小。我希望能够迭代块中的循环,以便它更有效地使用缓存,但是我不知道这是可能的。

我已经尝试了一种常规的阻塞方法,但是显然这并不是循环的正确解决方案,因为j每次从0开始迭代。

#define N     65536
#define BLOCK 4096

int min(int a, int b);

int main() {

  double a[N];
  double d_a;
  int i,j;
  int bbegin; 
  int bend;

  for(bbegin = 0;bbegin < N;bbegin += BLOCK) {

    bend = min(bbegin + BLOCK, N);

    for(i=bbegin;i<bend;i++){
      for(j=0;j<i;j++){

        /* array a has been filled up elsewhere */
        d_a = a[i] - a[j];
        /* do some stuff with d_a */

      }
    }
  }

}

inline int min(int a, int b) { return (a < b) ? a : b; }

1 个答案:

答案 0 :(得分:0)

更改:

import * as SettlementConstants from './constants'
import {connect} from 'react-redux'
import React, {useState, useEffect} from 'react'
import {Card, Table} from 'antd'
import PropTypes from 'prop-types'

const propTypes = {
  settlements: PropTypes.object.isRequired,
}

function Settlements(props) {

  const [selectedSettlementRows, setSelectedSettlementRows] = useState([])

  useEffect(() => {
    getSettlementDetails()
  }, [])

  function getSettlementDetails() {
    props.dispatch({
      type: SettlementConstants.GET_SETTLEMENT_PAYMENT_SUMMARIES,
      params: {
        'group_by': 'country_code',
        'type': SettlementConstants.CLAIM_SETTLEMENT,
      }
    })
    props.dispatch({
      type: SettlementConstants.GET_SETTLEMENT_PAYMENTS,
      params: {'type': SettlementConstants.CLAIM_SETTLEMENT, }
    })
  }

  const columns_payment_summary_table = [
    {
      title: SettlementConstants.LABEL_QUANTITY_SELECTED,
      dataIndex: 'group',
      key: 'group',
      render: text => (
        <span>{getCountForCountry(text)}</span>
      ),
    }
  ]

  function getCountForCountry(country_code){
    let selected_country = selectedSettlementRows.filter(function(row){
      return row.group === country_code
    })

    if(selected_country && selected_country.length > 0){
      return selected_country[0].ids.length
    } else {
      return 0
    }
  }

  return (
    <div>
      <Card
        title={SettlementConstants.LABEL_SETTLEMENT_SUMMARY}>
        <Table
          columns={columns_payment_summary_table}
          bordered={true}
          dataSource={props.settlements.settlement_payment_summaries}
          loading={props.settlements.settlement_payment_summaries_pending && !props.settlements.settlement_payment_summaries}
          rowKey={record => record.group}
        />
      </Card>
    </div>
  )
}

Settlements.propTypes = propTypes

const mapStateToProps = (state) => {
  return {settlements: state.settlementsReducer}
}

export default connect(
  mapStateToProps,
)(Settlements)

收件人:

for (i = bbegin; i < bend; i++)
    for (j = 0; j < i; j++)

这将处理相同的元素,但顺序不同,结果 当下一个for (j = 0; j < bend-1; j++) for (i = max(bbegin, j+1); i < bend; i++) 是 已加载,因此该块中的元素将保留在缓存中。

({a[i]当然具有明显的定义,类似于a[j]的展示。)