Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
/coreclrPublic archive

Improve Math.DivRem performance#8125

Merged
stephentoub merged 2 commits intodotnet:masterfromstephentoub:divrem_perf
Nov 15, 2016

Conversation

@stephentoub
Copy link
Member

Per the discussion athttps://github.com/dotnet/coreclr/issues/3439, this PR at least temporarily changes the implementation of Math.DivRem to avoid two idiv instructions. It then replaces a few % and / pairs in mscorlib with Math.DivRem.

Perf test:

usingSystem;usingSystem.Diagnostics;classProgram{staticlongdiv,rem,a,b;staticvoidMain(){// Various sets of a/b inputsvarpairs=new[]{Tuple.Create<long,long>(0,1),Tuple.Create<long,long>(4,2),Tuple.Create<long,long>(99,10),Tuple.Create<long,long>(-99,-10),Tuple.Create<long,long>(-99,10),Tuple.Create<long,long>(4,12),Tuple.Create<long,long>(1,long.MaxValue),Tuple.Create<long,long>(long.MaxValue,1),Tuple.Create<long,long>(long.MaxValue,3),Tuple.Create<long,long>(long.MaxValue,long.MaxValue-1),};constintIters=50000000;// Time the old and the new on each inputvarsw=newStopwatch();foreach(Tuple<long,long>tinpairs){a=t.Item1;b=t.Item2;sw.Restart();for(inti=0;i<Iters;i++)div=DivRem_Old(a,b,outrem);sw.Stop();TimeSpanoldTime=sw.Elapsed;sw.Restart();for(inti=0;i<Iters;i++)div=DivRem_New(a,b,outrem);sw.Stop();TimeSpannewTime=sw.Elapsed;Console.WriteLine($"Old:{oldTime} New:{newTime}");}}publicstaticlongDivRem_Old(longa,longb,outlongresult){result=a%b;returna/b;}publicstaticlongDivRem_New(longa,longb,outlongresult){longdiv=a/b;result=a-(div*b);returndiv;}}

outputs for me:

> corerun divremtest.exeOld: 00:00:01.0659336 New: 00:00:00.5490556Old: 00:00:01.0559024 New: 00:00:00.5516266Old: 00:00:01.0675624 New: 00:00:00.5485116Old: 00:00:01.0537601 New: 00:00:00.5483118Old: 00:00:01.0612407 New: 00:00:00.5526581Old: 00:00:01.0874783 New: 00:00:00.5545691Old: 00:00:01.1375759 New: 00:00:00.5528239Old: 00:00:01.0875608 New: 00:00:00.5720249Old: 00:00:01.1346930 New: 00:00:00.6171032Old: 00:00:01.1621859 New: 00:00:00.5689636

cc:@jkotas,@mikedn,@redknightlois

Until the JIT is able to eliminate one of the two idiv operations, using a multiplication and subtraction is measurably faster than an extra division.
@redknightlois
Copy link

I would aggresively inline it by default if it were my codebase.

@mikedn
Copy link

I'm not sure that usingDivRem when the divisor is constant is a good idea. The JIT already handles this partially and I have an open PR that will improve that.

@stephentoub
Copy link
MemberAuthor

I'm not sure that using DivRem when the divisor is constant is a good idea

Reverting those

Went through all uses of % looking for places DivRem could be used.  This looks like it's the only one of note.
@mikedn
Copy link

LGTM

1 similar comment
@redknightlois
Copy link

LGTM

@stephentoub
Copy link
MemberAuthor

@dotnet-bot test Ubuntu x64 Checked Build and Test please (https://github.com/dotnet/coreclr/issues/6574)

@stephentoubstephentoub merged commit68e3e94 intodotnet:masterNov 15, 2016
@stephentoubstephentoub deleted the divrem_perf branchNovember 15, 2016 22:22
@karelzkarelz modified the milestone:2.0.0Aug 28, 2017
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

1 more reviewer

@jkotasjkotasjkotas approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

2.0.0

Development

Successfully merging this pull request may close these issues.

6 participants

@stephentoub@redknightlois@mikedn@jkotas@karelz@dnfclas

[8]ページ先頭

©2009-2025 Movatter.jp