忘记密码   免费注册 查看新帖 | 论坛精华区

ChinaUnix.net

  平台 论坛 博客 认证专区 大话IT HPC论坛 徽章 文库 沙龙 自测 下载 频道自动化运维 虚拟化 储存备份 C/C++ PHP MySQL 嵌入式 Linux系统
最近访问板块 发新帖
查看: 457 | 回复: 1

求看下面的代码怎么降低cpu利用率,生成测试数据的脚本 [复制链接]

论坛徽章:
0
发表于 2017-06-08 18:18 |显示全部楼层
根据网上的脚本改编 了下genDataBase1 函数添加了70多个变量 然后 cpu利用率从0%升到了100%啊 哭死import random
import datetime
import time

dataCount = 10
codeRange = range(ord('a'), ord('z'))
alphaRange = [chr(x) for x in codeRange]
alphaMax = len(alphaRange)
daysMax = 365
theDay = datetime.date(2016, 6, 1)

d_KANSAI_KUBUN = ['1', '2']
d_URIAGE_NYURYOKU_KEIRO_KUBUN = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '97', '98', '99', '21', '22',
                               '23', '15']
d_KOKUSAI_BRAND_KUBUN = ['01', '02', '03', '04', '05', '98', '99']
d_URIAGE_AKAKURO_KUBUN = ['1', '2']
d_KAIGAI_GENCHI_TSUKA_CODE = ['AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA', 'ARS', 'AUD', 'AWG', 'AZN', 'BAM', 'BBD', 'BDT',
                            'BGN', 'BHD', 'BIF', 'BMD', 'BND', 'BOB']


def genRandomName(nameLength):
    global alphaRange, alphaMax
    length = random.randint(1, nameLength)
    name = ''
#    for i in range(length):
#        name += alphaRange[random.randint(0, alphaMax - 1)]
return name


def genRandomDay():
    global daysMax, theDay
    mDays = random.randint(0, daysMax)
    mDate = theDay + datetime.timedelta(days=mDays)
    return mDate.strftime('%Y%m%d')


def genRandTime():
    global daysMax, theDay
    mDays = random.randint(0, daysMax)
    mDate = theDay + datetime.timedelta(days=mDays)
    return mDate.strftime('%Y%m%d %H:%M:%S')


def genRandomSex():
    return random.randint(0, 1)


def genDataBase1(fileName, dataCount):
    outp = open(fileName, 'w')
    i = 0
    while i < dataCount:
        URIAGE_MEISAI_BANGO = i + 1
        KOKYAKU_KANRIBANGO = genRandomName(19)
        KEIYAKU_KANRIBANGO = i + 1
        KAIIN_BANGO = genRandomName(19)
        RIYO_NENGAPPI = genRandomDay()
        URIAGE_SHIHARAI_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        URIAGE_TORIATSUKAI_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        URIAGE_MEISAI_YUKO_MUKO_SIGN = genRandomName(1)
        URIAGE_NYURYOKU_KEIRO_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        KAMEITEN_BRAND_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        NETGAKU = random.uniform(1, 100)
        ZEISORYUO = random.uniform(1, 100)
        GROSSGAKU = random.uniform(1, 100)
        URIAGE_AKAKURO_KUBUN = random.choice(d_URIAGE_AKAKURO_KUBUN)
        SHIHARAI_HOHO_HENKO_NENGAPPI = genRandomDay()
        AKKR_MTNG_URIAGE_MEISAI_BANGO = i + 1
        AUTHORIZATION_SHONIN_BANGO = i + 1
        GIFT_CARD_MSHK_MEISAI_BANGO = i + 1
        KAMEITEN_JISHA_TASHA_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        KAIIN_URIAGE_TOROKU_SHIME_NNGP = genRandomDay()
        SHOKAI_KAIIN_SEIKYU_KKT_NNGP = genRandomDay()
        IDM_SHOZOKU_CODE = genRandomName(14)
        URIAGE_GANKIN_ZANDAKA = random.uniform(1, 100)
        URIAGE_GANKIN_NYUKIN_RKGK = random.uniform(1, 100)
        URIAGE_KANSAI_NENGAPPI = genRandomDay()
        KANSAI_KUBUN = random.choice(d_URIAGE_AKAKURO_KUBUN)
        SHIHARAI_HOHO_HENKO_RIYU_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        KYU_URIAGE_MEISAI_BANGO = genRandomName(19)
        SHIN_URIAGE_MEISAI_BANGO = genRandomName(19)
        ISSUER_KAISHA_CODE = genRandomName(3)
        KAMEITEN_KIGYO_BANGO = genRandomName(16)
        KAMEITEN_TEMPO_BANGO = genRandomName(16)
        KAMEITEN_TEMPO_MEISHO_KANJI = genRandomName(19)
        KAMEITEN_TEMPO_MEISHO_KANA = genRandomName(19)
        SHOHIN_CODE = genRandomName(19)
        KARI_POINTOSU = random.randint(1, 1000)
        URIAGE_TOTSUGO_UMU_SIGN = genRandomName(1)
        KOKUNAI_KAIGAI_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        KAIGAI_GENCHI_TSUKA_CODE = random.choice(d_KAIGAI_GENCHI_TSUKA_CODE)
        GENCHI_TSUKA_ENKANZAN_NENGAPPI = genRandomDay()
        KAIGAI_GENCHI_TSUKAGAKU = random.uniform(1, 100)
        KAIGAI_TOSHI_KANAMEISHO = genRandomName(19)
        HYOUJIYOU_KAMEITEN_KANJIMEI = genRandomName(19)
        URG_GNCH_TSUKA_SSTN_IKA_KETASU = genRandomName(1)
        MCC_CODE = genRandomName(10)
        ENKANZAN_RATE = random.uniform(1, 100)
        THREED_SECURE_NINSHO_KUBUN = random.choice(d_URIAGE_AKAKURO_KUBUN)
        VRTL_MALL_NISH_JOHO_ANGK_SIGN = genRandomName(1)
        ACQUIRER_SANSHO_BANGO = i + 1
        KKNI_TASHA_URGH_SHOUKAI_BANGO = i + 1
        POS_ENTRY_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        ACQUIRER_CARD_KAISHA_CODE = genRandomName(3)
        KOKUSAI_BRAND_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        TORIKESHI_HEMPIN_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        TORIKESHI_HEMPIN_SHORI_CODE = genRandomName(3)
        TORIKESHI_HEMPIN_RIYU_CODE = genRandomName(3)
        TORIHIKI_SHORI_JIKOKU = genRandomName(3)
        CD_RIYO_KAISHA_TAMMATSU_BANGO = genRandomName(3)
        CAFIS_KAISHA_CODE = genRandomName(3)
        JIKANGAI_TORIHIKI_TESURYO = random.uniform(1, 100)
        TORIKESHI_NYURYOKU_NENGAPPI = genRandomName(3)
        TORIKESHI_TORIHIKI_KEIRO_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        TORIKESHI_SIGN = genRandomName(1)
        TORIKESHI_RIYU_CODE = genRandomName(3)
        CARDKEIYAKU_SHOHIN_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        TESURYO_SHURUI_KUBUN = random.choice(d_KOKUSAI_BRAND_KUBUN)
        CREATEDAT = genRandTime()
        CREATEDBY = genRandomName(3)
        CREATEDEVENTID = genRandomName(3)
        UPDATEDAT = genRandTime()
        UPDATEDBY = genRandomName(3)
        UPDATEDEVENTID = genRandomName(3)
        UPDATE_COUNTER = random.randint(1, 1000)
        LOGICALDELETESIGN = genRandomName(1)
        MINASHI_NICHIJI = genRandTime()
        SYORI_KAISHI_NICHIJI = genRandTime()
        JIKKOU_USER = 'batch_user'
# mLine = "%i %s %s %s %d\n"%(i+1,firstName,lastName,birthday,sex)
mLine = "%025i,%s,%019i,%s,%s,%s,%s,%s,%s,%s,%0.5f,%0.5f,%0.5f,%s,%s,%025i,%07i,%012i,\
      %s,%s,%s,%s,%0.5f,%0.5f,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%i,%s,%s,%s,%s,%0.5f,%s,%s,%s,\
      %s,%0.3f,%s,%s,%025i,%025i,%s,%s,%s,%s,%s,%s,%s,%s,%s,%0.5f,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,\
      %s,%s,%i,%s,%s,%s,%s\n" % (
        URIAGE_MEISAI_BANGO, KOKYAKU_KANRIBANGO, KEIYAKU_KANRIBANGO, KAIIN_BANGO, RIYO_NENGAPPI, URIAGE_SHIHARAI_KUBUN,
        URIAGE_TORIATSUKAI_KUBUN, URIAGE_MEISAI_YUKO_MUKO_SIGN, URIAGE_NYURYOKU_KEIRO_KUBUN, KAMEITEN_BRAND_KUBUN,
        NETGAKU, ZEISORYUO, GROSSGAKU, URIAGE_AKAKURO_KUBUN, SHIHARAI_HOHO_HENKO_NENGAPPI,
        AKKR_MTNG_URIAGE_MEISAI_BANGO, AUTHORIZATION_SHONIN_BANGO, GIFT_CARD_MSHK_MEISAI_BANGO,
        KAMEITEN_JISHA_TASHA_KUBUN, KAIIN_URIAGE_TOROKU_SHIME_NNGP, SHOKAI_KAIIN_SEIKYU_KKT_NNGP, IDM_SHOZOKU_CODE,
        URIAGE_GANKIN_ZANDAKA, URIAGE_GANKIN_NYUKIN_RKGK, URIAGE_KANSAI_NENGAPPI, KANSAI_KUBUN,
        SHIHARAI_HOHO_HENKO_RIYU_KUBUN, KYU_URIAGE_MEISAI_BANGO, SHIN_URIAGE_MEISAI_BANGO, ISSUER_KAISHA_CODE,
        KAMEITEN_KIGYO_BANGO, KAMEITEN_TEMPO_BANGO, KAMEITEN_TEMPO_MEISHO_KANJI, KAMEITEN_TEMPO_MEISHO_KANA,
        SHOHIN_CODE, KARI_POINTOSU, URIAGE_TOTSUGO_UMU_SIGN, KOKUNAI_KAIGAI_KUBUN, KAIGAI_GENCHI_TSUKA_CODE,
        GENCHI_TSUKA_ENKANZAN_NENGAPPI, KAIGAI_GENCHI_TSUKAGAKU, KAIGAI_TOSHI_KANAMEISHO, HYOUJIYOU_KAMEITEN_KANJIMEI,
        URG_GNCH_TSUKA_SSTN_IKA_KETASU, MCC_CODE, ENKANZAN_RATE, THREED_SECURE_NINSHO_KUBUN,
        VRTL_MALL_NISH_JOHO_ANGK_SIGN, ACQUIRER_SANSHO_BANGO, KKNI_TASHA_URGH_SHOUKAI_BANGO, POS_ENTRY_KUBUN,
        ACQUIRER_CARD_KAISHA_CODE, KOKUSAI_BRAND_KUBUN, TORIKESHI_HEMPIN_KUBUN, TORIKESHI_HEMPIN_SHORI_CODE,
        TORIKESHI_HEMPIN_RIYU_CODE, TORIHIKI_SHORI_JIKOKU, CD_RIYO_KAISHA_TAMMATSU_BANGO, CAFIS_KAISHA_CODE,
        JIKANGAI_TORIHIKI_TESURYO, TORIKESHI_NYURYOKU_NENGAPPI, TORIKESHI_TORIHIKI_KEIRO_KUBUN, TORIKESHI_SIGN,
        TORIKESHI_RIYU_CODE, CARDKEIYAKU_SHOHIN_KUBUN, TESURYO_SHURUI_KUBUN, CREATEDAT, CREATEDBY, CREATEDEVENTID,
        UPDATEDAT, UPDATEDBY, UPDATEDEVENTID, UPDATE_COUNTER, LOGICALDELETESIGN, MINASHI_NICHIJI, SYORI_KAISHI_NICHIJI,
        JIKKOU_USER)
    outp.write(mLine)
    i += 1

    outp.close()


if __name__ == "__main__":
    random.seed()
    start = time.time()
    genDataBase1('db_test.txt', dataCount)
    end = time.time()
    print('use time:%d' % (end - start))
    print('Ok')

论坛徽章:
0
发表于 2017-06-08 18:49 |显示全部楼层
找到原因了,原来是后面的outp.write(mLine) 缩进搞错了 ....对python不熟悉啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

SACC2017购票8.8折优惠进行时

2017中国系统架构师大会(SACC2017)将于10月19-21日在北京新云南皇冠假日酒店震撼来袭。今年,大会以“云智未来”为主题,云集国内外顶级专家,围绕云计算、人工智能、大数据、移动互联网、产业应用等热点领域展开技术探讨与交流。本届大会共设置2大主会场,18个技术专场;邀请来自互联网、金融、制造业、电商等多个领域,100余位技术专家及行业领袖来分享他们的经验;并将吸引4000+人次的系统运维、架构师及IT决策人士参会,为他们提供最具价值的交流平台。
----------------------------------------
优惠时间:2017年8月2日前

活动链接>>
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号 北京市公安局海淀分局网监中心备案编号:11010802020122
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员  联系我们:
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP